[英]Fill: SelectCommand.Connection property has not been initialized C#
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con=new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
cmd.CommandText="select id from regtb where id="+Session["id"];
da.SelectCommand = cmd;
da.Fill(dt);
if(dt.Rows.Count>0)
{
Response.Redirect("userlogin.aspx");
}
}
What is wrong with this code?这段代码有什么问题? can anybody help me on this i am getting this error and i dont know how to correct this.
任何人都可以帮我解决这个问题,我收到了这个错误,我不知道如何纠正这个错误。
You will need to open your SqlConnection and then assign it to the SqlCommand:您需要打开 SqlConnection,然后将其分配给 SqlCommand:
con.Open();
cmd.Connection = con;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.