我在本地沒有問題。但是我在上傳的網站中發現了這個錯誤:ASP.NET對象引用未設置爲對象的實例
未將對象引用設置爲對象的實例。
在this鏈接它說這是一個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(對象發件人,ImageClickEventArgs E)601
的System.Web.UI .WebControls.ImageButton.OnClick(ImageClickEventArgs E)115
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(字符串eventArgument)120System.Web.UI.WebControls.ImageButton.System.Web.UI .IPostBackEve ntHandler.RaisePostBackEvent(字符串 eventArgument)10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串eventArgument)13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection中POSTDATA)36
系統.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
}
謝謝你的幫助。
除了別的,你應該使用'using'語句關閉你的命令和連接。 –
你能發表你得到那個例外的哪一行代碼嗎? –
@NickW:我不知道如何逐行調試它,因爲它不是本地的,我上傳了它。 –