填充:selectcommand.connection属性尚未初始化

时间:2013-12-10 14:23:35

标签: c#-4.0 stored-procedures

我在sda.Fill(dt)上收到错误;那个Fill:selectcommand.connction属性尚未初始化可能是连接中的问题?

 protected void Updatecustomer(object sender, GridViewUpdateEventArgs e)
{

    string cstid = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblcustomerid")).Text;
    string csnm = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname")).Text;
    string cmpn = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcompany")).Text;
    SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\LENOVO\\Documents\\Visual Studio 2010\\WebSites\\LiveSms\\App_Data\\Sms.mdf;Integrated Security=True;User Instance=True");
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "update gridCompany set name=@name,company=@company where cid=@cid;
    cmd.Parameters.Add("@cid", SqlDbType.NVarChar).Value = cstid;
    cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = csnm;
    cmd.Parameters.Add("@comnpany", SqlDbType.NVarChar).Value = cmpn;
    GridView1.EditIndex = -1;
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter();
    sda.SelectCommand = cmd;
    sda.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

0 个答案:

没有答案