Loading ...

Error When Data Binding A Repeater Control Who's Data Source Has Changed

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  web forms / data controls   » Error When Data Binding A Repeater Control Who's Data Source Has Changed

Error When Data Binding A Repeater Control Who's Data Source Has Changed

Posts under the topic: Error When Data Binding A Repeater Control Who's Data Source Has Changed

Posted: 7/4/2010

Lurker 10  points  Lurker
  • Joined on: 7/4/2010
  • Posts: 2

The logic in a nutshell is I have a sqlDataSource that gets some data. Then a repeater control uses that data source to display a list on the page. Then the user can add to this list by typing in a textfield and clicking a button. Everything displays fine on the initial page load. The user types in some data to add to the list and clicks a button. To avoid a complete page post back I use a ajax event to capture the click and insert the user's data into the db. Then I call Select on the sqlDataSource to grab the data from the db again. Then I call DataBind on the repeater to refresh the list and display the data agian. This is where the error occurs. I beleive the error occurs because on the initial page load the sqlDataSource returns (lets say) 2 records and the repeater creates 2 controls from the item template to display the 2 records. Then the user adds some data and clicks the button. My code inserts the data into the db and then the Select call on the sqlDataSource retuns (now) 3 records but the DataBind call on the repeater binds the first two records and then chokes and pukes on the third because there is only 2 controls in the repeater. I need the repeater to rerender and create (in this example) 3 controls form its item template.

What I'm I missing here or is this even the reason I get the error.


Posted: 7/4/2010

Professional 8495  points  Professional
  • Joined on: 5/3/2010
  • Posts: 389
  Answered

Hello Tyler and Welcome to the CodeASP.NET Community.

I think you are doing something wrong in your code. Probably showing the necessary parts of your code will be crucial for us to give you good advices and possible solutions.

However, I have created one simple solution which does what you (as I think) you need.

ASPX code

    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="ADD" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
                <ItemTemplate>
                    <%# Eval("Item") %> <br /> <!-- replace your ItemTemplate aspx code as appropriate -->
                </ItemTemplate>
            </asp:Repeater>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MySampleDBConnectionString %>" 
                SelectCommand="SELECT [Item] FROM [Item]"></asp:SqlDataSource>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>


If you see, I'm using ScriptManager and UpdatePanel. Have one Button ADD and TextBox to add it in the Repeater. In the Repeater, I have one <%# Eval('Item') %> which I get from the SqlDataSource's select command retrieved from database. You should replace your own commands, Repeater ItemTemplate ASPX code and the Connection String in the SqlDataSource.

Then, In code behind everything you need to do is the following.

On Button1_Click method

            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["MySampleDBConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("insert into Item values ('" + TextBox1.Text + "')", con);
            using (con)
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            //replace above code with your code for INSERTING INTO DATABASE TABLE
           //then just call the Repeater1.DataBind();, nothing else.


            Repeater1.DataBind();

Hope this helps.


Posted: 7/5/2010

Lurker 10  points  Lurker
  • Joined on: 7/4/2010
  • Posts: 2
Thanks for the reply hajan and your code ran correctly but my project is a little more complex than I first wrote. 
 
I'm using an ExtJS control as an inline editor to capture the user's add action. ExtJS uses its own script manager and I think this might conflict with an update panel's script manager. Anyway I'll post the code and would appreciate any help or advice you could give me. (NOTE: The code here is own a content page, the master page has both script manager's for the  AJAX update panel and the ExtJS controls.)

<!-- Scripts -->

<script runat="server" type="text/VB">

Protected Function CompleteAdd(ByVal sender As Object, ByVal e As AjaxEventArgs)

lblAddDateCode.Text = ""

Dim sds As New SqlDataSource

sds.ConnectionString = "Data Source=Cognos;Initial Catalog=QEITWEB;Integrated Security=True"

sds.UpdateCommandType = SqlDataSourceCommandType.Text

sds.UpdateCommand = "EXECUTE [dbo].[sp_UPDATE_RMR_STAGING_TABLES] '" & rmrNumber & "', '" & e.ExtraParams("rmrTable") & "', '" & e.ExtraParams("rmrColumn") & "', '" & e.ExtraParams("value") & "', '{none}';"

sds.Update()

sdsGetRmrDateCodes.Select(DataSourceSelectArguments.Empty)

repDateCodes.DataBind()

udpDateCodes.Update()

End Function

</script>

 

<!-- Date Codes Panel -->

<ext:Panel ID="pnlDateCodes" runat="server" Title="Date Codes" Cls="sectionPanel" Frame="True" Icon="Calendar">

<Body>

<asp:UpdatePanel ID="udpDateCodes" runat="server" UpdateMode="Conditional">

<ContentTemplate>

<!-- Date Codes Table -->

<table cellpadding="0" cellspacing="0" class="panelTable">

<!-- Date Codes Row -->

<tr class="dataRows">

<td class="labelFields">

<!-- Date Codes Label -->

<asp:Label ID="lblDateCodes" runat="server" Text="Date Codes:&nbsp&nbsp" />

</td>

<td class="dataFields">

<table>

<!-- Date Codes Repeater -->

<asp:Repeater ID="repDateCodes" runat="server" DataSourceID="sdsGetRmrDateCodes" >

<ItemTemplate>

<tr class="dataRows">

<td class="dataFields">

<!-- Date Codes Control -->

<ext:Label

ID="lblDateCodesData"

runat="server"

Cls="editable"

Icon="NoteEdit"

IconAlign="Left"

OverCls="editable-over"

EmptyText="Click here to edit"

Text='<%# Eval("RMB_BUILD_DATE") %>'>

<Editor>

<ext:Editor ID="edtDateCode" runat="server">

<AjaxEvents>

<StartEdit OnEvent="StartEdit">

<ExtraParams>

<ext:Parameter Name="oldValue" Value="value" Mode="Raw"/>

</ExtraParams>

</StartEdit>

<Complete OnEvent="CompleteEdit">

<ExtraParams>

<ext:Parameter Name="rmrTable" Value="RMR_BUILD_DATE_STG" Mode="Value" />

<ext:Parameter Name="rmrColumn" Value="RMB_BUILD_DATE" Mode="Value" />

<ext:Parameter Name="value" Value="value" Mode="Raw" />

</ExtraParams>

</Complete>

</AjaxEvents>

<Alignment ElementAnchor="Left" TargetAnchor="Left" />

<Field>

<ext:TextField

ID="txfDateCode"

runat="server"

Cls="x-form-field-editor"

AllowBlank="false"

SelectOnFocus="true"

Width="200px"

MaxLength="20" />

</Field>

</ext:Editor>

</Editor>

</ext:Label>

</td>

<td>

<!-- Delete Command Control -->

<ext:Button ID="Button1" runat="server" Icon="Cross" Flat="True" />

</td>

</tr>

</ItemTemplate>

</asp:Repeater>

<!-- Add Command Control -->

<tr>

<td>

<ext:Label

ID="lblAddDateCode"

runat="server"

Cls="editable"

Icon="ControlAddBlue"

IconAlign="Left"

OverCls="editable-over"

EmptyText="Add Another">

<Editor>

<ext:Editor ID="edtAddDateCode" runat="server">

<AjaxEvents>

<Complete OnEvent="CompleteAdd">

<ExtraParams>

<ext:Parameter Name="rmrTable" Value="RMR_BUILD_DATE_STG" Mode="Value" />

<ext:Parameter Name="rmrColumn" Value="RMB_BUILD_DATE" Mode="Value" />

<ext:Parameter Name="value" Value="value" Mode="Raw" />

</ExtraParams>

</Complete>

</AjaxEvents>

<Alignment ElementAnchor="Left" TargetAnchor="Left" />

<Field>

<ext:TextField

ID="TextField2"

runat="server"

Cls="x-form-field-editor"

AllowBlank="false"

SelectOnFocus="true"

Width="200px"

MaxLength="20"/>

</Field>

</ext:Editor>

</Editor>

</ext:Label>

</td>

</tr>

</table>

</td>

</tr>

</table>

<!-- Date Codes Data Source -->

<asp:SqlDataSource ID="sdsGetRmrDateCodes" runat="server"

ConnectionString="<%$ ConnectionStrings:QEITWEBConnectionString %>"

SelectCommandType="StoredProcedure"

SelectCommand="sp_GET_RMR_DATA">

<SelectParameters>

<asp:QueryStringParameter DefaultValue="20100405-01" Name="RMR_NUMBER"

QueryStringField="rmrNumber" Type="String" />

<asp:Parameter DefaultValue="DATE CODES" Name="DATA_TYPE" Type="String" />

</SelectParameters>

</asp:SqlDataSource>

</ContentTemplate>

</asp:UpdatePanel>

</Body>

</ext:Panel>

 

 
 
 
 
 

Posted: 7/5/2010

Professional 8495  points  Professional
  • Joined on: 5/3/2010
  • Posts: 389

Well, I don't have the ExtJS right on this machine, but will test it later or tomorrow, surely.

Meanwhile, instead of using the SqlDataSource to execute the update command, try doing that using standard SqlCommand as I did that in my previous post.

Use the SqlDataSource just to select the data for the Repeater.

There is possibility that the ScriptManagers of both AJAX frameworks might make conflict, but let met first test that then will reply shortly with my suggestion.

Also, I see you are calling the CompleteAdd method on Complete event of your Ext Editor. You should take a look around there. I'm not sure if it is a suitable event for calling that method.

Again, I will reply once I try the code.

Regards,
Hajan


Page 1 of 1 (4 items)