0

ローカルでは問題ありません。しかし、アップロードされたウェブサイトで次のエラーが発生します。

オブジェクト参照がオブジェクト インスタンスに設定されていません。

このリンクでは、これは ADO のバグであると書かれていますが、コードで ADO を使用していません。

これが私のコードです:

        SqlConnection sc = new SqlConnection();
        sc.ConnectionString = MyConnectionString;
        sc.Open();
        SqlCommand scm = new SqlCommand(INSERT COMMAND);
        scm.Connection = sc;
        scm.ExecuteNonQuery();
        sc.Close();

上記のコードをUSINGステートメントに入れましたが、何も変わりませんでした。

ここに私のスタックトレースがあります


NullReferenceException
:オブジェクト参照がオブジェクトのインスタンスに設定されていません
。 .ImageButton.RaisePostBackEvent(String eventArgument) +120

System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page .RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(ブール値 includeStagesBeforeAsyncPoint、ブール値 includeStagesAfterAsyncPoint) +5563

BtnRegister_Clickコードは次のとおりです。

    protected void BtnRegister_Click(object sender, ImageClickEventArgs e)
    {
    if (TxtName.Text == "" || TxtFamily.Text == "" || txtNationalCode.Text == "" || TxtIdNumber.Text == "" || txtMobile.Text == "" || TxtEmail.Text == "" || txtAddress.Text == "" || TxtSecurityCode.Text == "")
    {
        LblMsg.Text = "You should fill all fields";
        return;
    }

    if (txtNationalCode.Text.Length != 10)
    {
        LblMsg.Text = "National code must have 10 digits";
        return;
    }

    if (txtMobile.Text.Length != 11)
    {
        LblMsg.Text = "Mobile number should have 11 digits";
        return;
    }

    if (T_Paticipant.GetDataByNationalCode(txtNationalCode.Text).Rows.Count > 0)
    {
        LblMsg.Text = "This national code used before";
        return;
    }

    if (T_Paticipant.GetDataByEmail(TxtEmail.Text).Rows.Count > 0)
    {
        LblMsg.Text = "This email used before";
        return;
    }

    LblMessage.Text = "Are you sure to register?";
    LblFlag.Text = "0";


    //captcha

    if (this.Session["CaptchaImageText"].ToString().ToLower() == TxtSecurityCode.Text.ToLower())
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "showMessage()", "<script>ShowMessage() </script>");
    }
    else
    {
        LblMsg.ForeColor = System.Drawing.Color.Red;
        LblMsg.Text = "Security code is wrong";
    }
    //end captcha
}

ご協力ありがとう御座います。

4

3 に答える 3

2

connectionstring開く前に最初に設定します

    SqlConnection sc = new SqlConnection();
    sc.ConnectionString = MyConnectionString
    SqlCommand scm = new SqlCommand(INSERT COMMAND);
    scm.Connection = sc;           // missing this.
    sc.Open();
    scm.ExecuteNonQuery();
    sc.Close();

また

using (SqlConnection conn = new SqlConnection("connectionstringhere"))
{
    using (SqlCommand comm = new SqlCommand())
    {
        comm.Connection = conn;
        comm.CommandText = "your query";
        comm.CommandType = CommandType.Text;
        // comm.Parameters.AddWithValue("@paramName","value"); // if you have parameters
        try
        {
            conn.Open();
            comm.ExecuteNonQuery();
        }
        catch(SqlException ex)
        {
            // error here
            // ex.Message.Tostring  // holds the error message
        }
    }
}
于 2012-10-13T08:07:10.620 に答える
0

みんな助けてくれてありがとう。このエラーは、aの値sessionがnullであり、ToString()メソッドが変換するデータを見つけることができなかったために発生しました。

于 2012-10-13T16:54:04.360 に答える
0

そのはずnew SqlCommand(query, sc)

于 2012-10-13T08:12:55.240 に答える