简体   繁体   English

如何解决Fill:SelectCommand.Connection属性尚未初始化

[英]how to solve Fill: SelectCommand.Connection property has not been initialized

I have a class which I have written all my methods there. 我有一个课,我在那里写了我所有的方法。 then I have my firstpage of web application which I have some codes there. 然后我有我的第一页Web应用程序,我在那里有一些代码。 I face this 我面对这个

  • $exception {"Fill: SelectCommand.Connection property has not been initialized."} System.Exception {System.InvalidOperationException} $ exception {“Fill:SelectCommand.Connection属性尚未初始化。”} System.Exception {System.InvalidOperationException}

in the line : 在线:

sda.Fill(dsUsers.tblMembers);

here is my code so I hope u can help me: 这是我的代码,所以我希望你能帮助我:

namespace MosquesNetwork
{
public class cUsers2
{
    SqlConnection scn;
    SqlDataAdapter sda;
    SqlCommandBuilder scb;
    SqlCommand SqlStr;


    public cUsers2()
    {
        SqlConnection scn = new SqlConnection (ConfigurationManager.ConnectionStrings["MosquesDBConnectionString"].ConnectionString);

        sda = new SqlDataAdapter();
        scb = new SqlCommandBuilder(sda);


    }

public bool CheckUserName(string UserName)
    {
        DsUsers2 dsUsers=new DsUsers2();
         bool Success;
        string SqlStr="select * from tblUsers where Username='"+UserName+"' ";
        sda.SelectCommand=new SqlCommand(SqlStr, scn);
        sda.Fill(dsUsers.tblMembers);
        sda.Fill(dsUsers.tblMembers);
        Success=dsUsers.tblMembers.Rows.Count>0;
        return Success;
    }

then I have this code while the login button is pressed in webform: 然后我在webform中按下登录按钮时有这个代码:

 protected void Button1_Click(object sender, EventArgs e)
    {

        if (txtuser.Text.Trim().Length>0 && txtpass.Value.Length>0 )
        {  
            cUsers2 cUsers=new cUsers2();
            DsUsers2 dsUser=new DsUsers2();
            bool Success;
            if (txtuser.Text.Trim()=="admin")
            {
                Success=cUsers.CheckAdminPass(txtuser.Text.Trim(),txtpass.Value.Trim());  
                if (Success)
                {
                    Response.Redirect("WebForm2.aspx");
                }
            }

            dsUser=cUsers.Checkpassword(txtuser.Text.Trim(), txtpass.Value.Trim(),out Success);
            if(Success)
            {
                Session["ID"]=dsUser.tblMembers.Rows[0][dsUser.tblMembers.IDUserColumn].ToString();
                System.Web.HttpContext.Current.Response.Redirect("frmProfile.aspx");

            }
            else lblerror.Text="invalid username & password";

        }
    }

Look at your constructor: 看看你的构造函数:

SqlConnection scn = new SqlConnection(...);

That's declaring a separate scn local variable, so the scn instance variable is staying as null. 这是声明一个单独的 scn局部变量,因此scn 实例变量保持为null。 If you just change it to: 如果您只是将其更改为:

scn = new SqlConnection(...);

that may well fix the immediate problem. 这可能很好地解决了眼前的问题。 It's not a nice design in my view - I'd prefer to create the SqlConnection in a using block where you actually need it - but that's what's going wrong for now... 这不是在我看来,一个不错的设计-我宁愿创建SqlConnectionusing块,你真的需要它-但是这是什么错误,现在...

I got also this problem. 我也遇到了这个问题。 after search the problem found that the object of connection that i have provide SqlDataAdapter is not Initialize so the connection passed as null 搜索后问题发现我提供SqlDataAdapter的连接对象不是Initialize,所以连接传递为null

//Try To Fetch The Value Like this...
protected void Search_Emp_Click(object sender, EventArgs e)
{
    SqlCommand cmd;
    SqlConnection con;
    SqlDataAdapter ad;
    con = new SqlConnection(); //making instance of SqlConnection

    con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString; //Invoking Connection String

    con.Open(); //Opening Connection

    if (Ddl_Emp_Search.SelectedItem.Text == "Date Of Joining")
    {
        cmd = new SqlCommand("Select Emp_Name,Contact_No,City,Emp_Desig,Place_Code from emp_registration where Emp_Doj=@Emp_Doj",con);
        cmd.Parameters.Add("@Emp_Doj", SqlDbType.NVarChar, 50).Value = TextBox1.Text;
    }
    else if (Ddl_Emp_Search.SelectedItem.Text == "Name")
    {
        cmd = new SqlCommand("Select Emp_Name,Place_Code,Emp_Code,Emp_Desig from emp_registration where Emp_Name=@Emp_Name", con);
        cmd.Parameters.Add("@Emp_Name", SqlDbType.NVarChar, 50).Value = TextBox1.Text;
    }
    else if (Ddl_Emp_Search.SelectedItem.Text == "Employee Code")
    {
        cmd = new SqlCommand("Select Emp_Name,Contact_No,City,Emp_Desig,Place_Code from emp_registration where Emp_Code=@Emp_Code", con);
        cmd.Parameters.Add("@Emp_Code", SqlDbType.NVarChar, 50).Value = TextBox1.Text;
    }
    else if (Ddl_Emp_Search.SelectedItem.Text == "City")
    {
        cmd = new SqlCommand("Select Emp_Name,Contact_No,City,Emp_Desig,Place_Code from emp_registration where City=@City", con);
        cmd.Parameters.Add("@City", SqlDbType.NVarChar, 50).Value = TextBox1.Text;
    }
    else
    {
        Response.Write("There is Something Worng....");
    }

    ad = new SqlDataAdapter(cmd); // Here IS THE PROBLEM WHEN YOU DOES'NT PASS CMD (Command instance) to the Data Adapter then there is a problem of ( Fill: SelectCommand.Connection property has not been initialized)

    DataSet ds = new DataSet();
    ad.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

Vishal Sir, If You are Getting that Problem then Be sure whether you are passing the 'cmd' to the SQL DATA ADAPTER. Vishal先生,如果您遇到了这个问题那么请确定您是否将'cmd'传递给SQL DATA ADAPTER。 for eg: 例如:

ad = new SqlDataAdapter(cmd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误填充:SelectCommand.Connection属性尚未初始化 - error Fill: SelectCommand.Connection property has not been initialized 填充:SelectCommand.Connection 属性尚未初始化 C# - Fill: SelectCommand.Connection property has not been initialized C# 填充: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? 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.” SelectCommand.Connection属性尚未初始化。 MySQL的 - The SelectCommand.Connection property has not been initialized. MySql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM