当我运行时发生错误,如下所示SelectCommand.Connection属性尚未初始化.

问题描述:

这是我的代码,如下所示:

This is my code as follows:

con.Open();
string strQuery = "select Sno,Name,Age,Address" +
             " from Details";
SqlCommand cmd = new SqlCommand(strQuery);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();


当我运行此错误时,将发生以下错误:


When I run this an error occurs as follows:

SelectCommand.Connection property has not been initialized.


该行中发生错误.


The error occurs in this line;

adp.Fill(ds);

添加以下行:
add this line:
cmd.Connection=con;