2012-10-13 56 views
0

我在本地沒有問題。但是我在上傳的網站中發現了這個錯誤: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)120

System.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 
} 

謝謝你的幫助。

+5

除了別的,你應該使用'using'語句關閉你的命令和連接。 –

+0

你能發表你得到那個例外的哪一行代碼嗎? –

+0

@NickW:我不知道如何逐行調試它,因爲它不是本地的,我上傳了它。 –

回答

0

謝謝大家的幫助。 發生此錯誤是因爲session的值爲空,而ToString()方法找不到要轉換的任何數據。

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 
     } 
    } 
} 
+0

我很抱歉,但在我的代碼中是正確的。在這裏我寫錯了 –

+0

@masoudkeshavarz檢查我更新的答案。設置連接到你的命令。 –

+1

@masoudkeshavarz:你*必須*確保你將來發布你的實際代碼。我們毫無意義地發現無關代碼中的錯誤。值得多花幾分鐘時間來確保您的問題在發佈之前儘可能好。請閱讀http://tinyurl.com/so-hints –

0

之前,它應該是new SqlCommand(query, sc)

相關問題