簡體   English   中英

ASP.NET Object引用未設置為對象的實例

[英]ASP.NET Object reference not set to an instance of an object

我在當地沒有問題。 但我在上傳的網站上收到此錯誤:

你調用的對象是空的。

這個鏈接中,它說這是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:對象引用未設置為對象的實例。]
Register.BtnRegister_Click(Object sender,ImageClickEventArgs e)+601
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)+115
System.Web.UI.WebControls.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(Boolean includeStagesBeforeAsyncPoint,Boolean 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
}

謝謝你的幫助。

在打開之前首先設置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
        }
    }
}

它應該是new SqlCommand(query, sc)

謝謝大家的幫助。 發生此錯誤是因為session值為null並且ToString()方法找不到要轉換的任何數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM