1

I have defined a SqlDataSource in my ASP code and I am trying to access it from my C# code-behind. I get an error that the source is unknown. What do I have to do so that I can access it from my code-behind?

C# code -

protected void OnEditingGridView1(object sender, GridViewEditEventArgs e)
{
    GridViewRow row = GridView1.Rows[e.NewEditIndex];
    int index = row.RowIndex;
    string ClientKey = GridView1.DataKeys[index].Values["ClientKey"].ToString();

    SqlDataSourceDebtor.SelectParameters.Clear();
    SqlDataSourceDebtor.SelectParameters.Add("clientKey", ClientKey);

}

ASP code -

            <EditItemTemplate>
                <asp:DropDownList ID="ddlDebtorName" runat="server" 
                    DataSourceID="SqlDataSourceDebtor" DataTextField="Name" 
                    DataValueField="DebtorKey">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSourceDebtor" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:AuditDevConnectionString2 %>" 
                    SelectCommand="sp_fc_vm_getDebtorList" SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="GridView1" DefaultValue="0" Name="ClientKey" 
                            PropertyName="SelectedValue" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </EditItemTemplate>
0

1 Answer 1

2

The SqlDataSource is in an EditItemTemplate. That means there'll be a SqlDataSource for each row of this GridView, Repeater—whatever this is.

If you want to get the SqlDataSource for, say, the third row in your GridView, you'd want something like:

gv.Rows[2].FindControl("SqlDataSourceDebtor") as SqlDataSource;

EDIT - I did a quick test and this works fine:

    <asp:GridView runat="server" ID="gv">
        <Columns>
            <asp:BoundField DataField="str" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList runat="server" DataTextField="name" DataValueField="id" DataSourceID="sqlId"></asp:DropDownList>
                    <asp:SqlDataSource runat="server" ID="sqlId" ConnectionString="Data Source=DevAdam;Initial Catalog=ZoomieRest;Integrated Security = true;"
                     SelectCommand="Select top 10 id, name from zoomiesql.subjects where userid = 7"></asp:SqlDataSource>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Code Behind

    gv.DataSource = new[] { new { str = "A" }, new { str = "B" }, new { str = "C" } };
    gv.DataBind();

    var sqlSource = gv.Rows[2].FindControl("sqlId") as SqlDataSource;
    Response.Write(sqlSource.ConnectionString);

This prints out the connection string for the embedded SqlDataSource.

Sign up to request clarification or add additional context in comments.

4 Comments

I am getting a null for the sqlSource variable.
@Craig - well that sql data source is in your edit template - make sure this row is in edit mode before trying to access it.
can you tell me which routine you were able to capture the above information? It looks as though in the OnEditing event, the row has not been shown yet.
Craig - I ran that code after data binding. I'm not sure after which event the edit temaplet is rendered

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.