简体   繁体   English

填充:SelectCommand.Connection 属性尚未初始化 C#

[英]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.

相关问题 C#错误:“填充:SelectCommand.Connection属性尚未初始化。” - C# Error: “Fill: SelectCommand.Connection property has not been initialized.” C#错误:“填充:SelectCommand.Connection属性尚未初始化。” - C# Error: “Fill: SelectCommand.Connection property has not been initialized.” C#错误:“填充:SelectCommand.Connection属性尚未初始化。”仅在PC上 - C# Error: “Fill: SelectCommand.Connection property has not been initialized.” just in on pc 错误填充:SelectCommand.Connection属性尚未初始化 - error Fill: SelectCommand.Connection property has not been initialized 如何解决Fill:SelectCommand.Connection属性尚未初始化 - how to solve Fill: SelectCommand.Connection property has not been initialized 填充:SelectCommand.Connection属性尚未初始化错误 - Fill: SelectCommand.Connection property has not been initialized error 填充:SelectCommand.Connection属性尚未初始化 - Fill: SelectCommand.Connection property has not been initialized 填充:SelectCommand.Connection 属性尚未初始化 - Fill: SelectCommand.Connection property has not been initialized SelectCommand.Connection属性尚未初始化 - SelectCommand.Connection property has not been initialized 填充:SelectCommand.Connection属性尚未初始化。 该怎么办? - Fill: SelectCommand.Connection property has not been initialized. what to do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM