1

我有一门课,我在那里写了我所有的方法。然后我有我的网页应用程序的第一页,我在那里有一些代码。我面对这个

  • $exception {“填充:SelectCommand.Connection 属性尚未初始化。”} System.Exception {System.InvalidOperationException}

在行中:

sda.Fill(dsUsers.tblMembers);

这是我的代码,所以我希望你能帮助我:

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;
    }

然后在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";

        }
    }
4

4 回答 4

3

看看你的构造函数:

SqlConnection scn = new SqlConnection(...);

那是声明一个单独 scn的局部变量,因此scn 实例变量保持为空。如果您只是将其更改为:

scn = new SqlConnection(...);

这很可能解决眼前的问题。在我看来,这不是一个好的设计——我更愿意在你真正需要它SqlConnectionusing块中创建它——但这就是现在的问题......

于 2012-09-21T10:59:02.157 回答
0

Vishal 先生,如果您遇到这个问题,请确定您是否将“cmd”传递给 SQL 数据适配器。例如:

ad = new SqlDataAdapter(cmd);
于 2013-11-30T13:31:17.493 回答
0
//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();
}
于 2013-11-13T17:44:44.837 回答
0

我也遇到了这个问题。搜索问题后发现我提供的 SqlDataAdapter 连接对象未初始化,因此连接传递为 null

于 2013-06-01T08:12:03.043 回答