0

I have a connection string in my web.config and a gridview. below is code for how I configured my gridview.

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" 

....

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:sb_cpdConnectionString %>" 
        SelectCommand="SELECT colID, lname, fname, address, hobby from table1 where column like '%' ">
<SelectParameters>
            <asp:ControlParameter ControlID="TextBox1" Name="lname" PropertyName="Text" 
                Type="String" />
            <asp:ControlParameter ControlID="TextBox1" Name="fname" PropertyName="Text" 
                Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

Since I want to show another gridview from my child data source and I am now using code behind, how do I bind my SqlDataSource to my databasae as I did in my aspx code.

Below is how I have done so far.

private SqlDataSource ChildDataSource(string strCustometId, string strSort)
{
    string strQRY = "";
    SqlDataSource ds = new SqlDataSource();
    string connectionString = WebConfigurationManager.ConnectionStrings["sb_cpdConnectionString"].ConnectionString;
    SqlConnection myConnection = new SqlConnection(connectionString);
    myConnection.Open();
strQRY = "SELECT [ID], [QuizNo]," +
                            "[Status],[CertificateNo],[Received] FROM [cpd_certificates]" +
                            " WHERE [ID] = '" + strCustometId + "'" +
                            "HAVING COUNT(*)=0 " + strSort;
   // this is where I need help to bind connection string.

I get an error: The connectionString property has been initialized.

1 Answer 1

4

You should assign the connection string to data source's connection string property

string connectionString = WebConfigurationManager.ConnectionStrings["sb_cpdConnectionString"].ConnectionString;
ds.ConnectionString = connectionString;
Sign up to request clarification or add additional context in comments.

Comments

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.