解決方法 : SelectCommand.Connection プロパティが初期化されていません。

私はクラスを持っていて、そこにすべてのメソッドを書きました。 そして、私はそこにいくつかのコードを持っているWebアプリケーションの私の最初のページを持っています。 私はこれに直面する

  • $exception {"Fill: SelectCommand.Connection property has not been initialized."} 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";

        }
    }

コンストラクタを見てください。

SqlConnection scn = new SqlConnection(...);

これはローカル変数 scn分離して 宣言しているので、scn インスタンス 変数は null のままになっています。もし、これを

scn = new SqlConnection(...);

に変更すれば、当面の問題は解決するかもしれません。私としては、SqlConnection を実際に必要とする using ブロックで作成する方が良いと思いますが、今のところうまくいっていません。

解説 (3)

私はまた、この問題を得た。問題を検索した後、私はSqlDataAdapterを提供している接続のオブジェクトが初期化されていないことが判明したので、接続はnullとして渡されます。

解説 (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();
}
解説 (3)