我的web.config和gridview中有一个连接字符串。下面是我如何配置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>
由于我想从我的子数据源显示另一个gridview,现在我正在使用代码,我如何将我的SqlDataSource绑定到我的数据库,就像我在aspx代码中那样。
以下是我到目前为止所做的工作。
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.
我收到错误:connectionString属性已初始化。
答案 0 :(得分:4)
您应该将连接字符串分配给数据源的连接字符串属性
string connectionString = WebConfigurationManager.ConnectionStrings["sb_cpdConnectionString"].ConnectionString;
ds.ConnectionString = connectionString;