2016-03-26 110 views
0

我試圖創建一個從C#到SQL Server的連接和NullReferenceException處理C#system.nullreferenceExceptionnot發生。用戶代碼

我已經嘗試了五次,但沒有找到治療我的問題。它說錯誤發生在line 3。我認爲這可能是SQL Server和C#代碼之間的連接問題。

這裏是C#代碼:登錄表單的

protected void Button1_Click(object sender, EventArgs e) 
{ 
    int userId = 0; 
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(constr)) 
    { 
     using (SqlCommand cmd = new SqlCommand("Insert_User")) 
     { 
      using (SqlDataAdapter sda = new SqlDataAdapter()) 
      { 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.Parameters.AddWithValue("@Username", TextBoxUN.Text.Trim()); 
       cmd.Parameters.AddWithValue("@Email", TextBoxEmail.Text.Trim()); 
       cmd.Parameters.AddWithValue("@Password", TextBoxPswd.Text.Trim()); 
       cmd.Connection = con; 
       con.Open(); 
       userId = Convert.ToInt32(cmd.ExecuteScalar()); 
       con.Close(); 
      } 
     } 
     string message = string.Empty; 
     switch (userId) 
     { 
      case -1: 
       message = "Username already exists.\\nPlease choose a different username."; 
       break; 
      case -2: 
       message = "Supplied email address has already been used."; 
       break; 
      default: 
       message = "Registration successful.\\nUser Id: " + userId.ToString(); 
       break; 
     } 
     ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true); 
    } 

下面的HTML代碼是:

<table class="auto-style1"> 
     <tr> 
      <td class="auto-style2">Username</td> 
      <td class="auto-style3"> 

       <asp:TextBox ID="TextBoxUN" runat="server" OnTextChanged="TextBoxUN_TextChanged" Width="180px"></asp:TextBox> 

      </td> 
      <td> 
       <asp:RequiredFieldValidator ID="Label1" runat="server" ControlToValidate="TextBoxUN" ErrorMessage="Username is required" ForeColor="Red"></asp:RequiredFieldValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="auto-style4">&nbsp;</td> 
      <td class="auto-style5">&nbsp;</td> 
      <td class="auto-style6">&nbsp;</td> 
     </tr> 
     <tr> 
      <td class="auto-style2">E-mail</td> 
      <td class="auto-style3"> 
       <asp:TextBox ID="TextBoxEmail" runat="server" Width="180px"></asp:TextBox> 
      </td> 
      <td> 
       <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBoxEmail" ErrorMessage="E-mail is required" ForeColor="Red"></asp:RequiredFieldValidator> 
       <br /> 
       <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBoxEmail" ErrorMessage="E-mail entered is not correct" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="auto-style2">Password</td> 
      <td class="auto-style3"> 
       <asp:TextBox ID="TextBoxPswd" runat="server" TextMode="Password" Width="180px"></asp:TextBox> 
      </td> 
      <td> 
       <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBoxPswd" ErrorMessage="Password is required" ForeColor="Red"></asp:RequiredFieldValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="auto-style2">Confirm-Password</td> 
      <td class="auto-style3"> 
       <asp:TextBox ID="TextBoxRPswd" runat="server" TextMode="Password" Width="180px"></asp:TextBox> 
      </td> 
      <td> 
       <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="TextBoxRPswd" ErrorMessage="Confirm your password" ForeColor="Red"></asp:RequiredFieldValidator> 
       <br /> 
       <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBoxPswd" ControlToValidate="TextBoxRPswd" ErrorMessage="Enter Correct Password" ForeColor="Red"></asp:CompareValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="auto-style7"></td> 
      <td class="auto-style8"></td> 
      <td class="auto-style9"></td> 
     </tr> 
     <tr> 
      <td class="auto-style2">&nbsp;</td> 
      <td class="auto-style3">&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
    </table> 
    <asp:Button ID="Button1" runat="server" BorderStyle="Ridge" OnClick="Button1_Click" Text="Register" /> 
    <input id="Reset1" type="reset" value="reset" /> 
+0

設置調試點,看看在哪一行錯誤實際發生的情況,然後指定你的問題就行了。 –

回答

0

我猜想錯誤是因爲SqlDataAdapter的,因爲你沒有任何任命。此外,您不需要一個SqlDataAdapter的一個值。你的代碼更改爲:

int userId = 0; 
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
using (SqlConnection con = new SqlConnection(constr)) 
{ 
    using (SqlCommand cmd = new SqlCommand("Insert_User", con)) 
    { 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@Username", TextBoxUN.Text.Trim()); 
     cmd.Parameters.AddWithValue("@Email", TextBoxEmail.Text.Trim()); 
     cmd.Parameters.AddWithValue("@Password", TextBoxPswd.Text.Trim()); 
     try 
     { 
      con.Open(); 
      userId = Convert.ToInt32(cmd.ExecuteScalar().ToString()); 
     } 
     catch(Exception ex) 
     { 
      Response.Write("<script language='javascript'>alert('" + ex.Message.ToString() + "');</script>");//It will throw an alert with an exception 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 
    string message = string.Empty; 
    switch (userId) 
    { 
     case -1: 
      message = "Username already exists.\\nPlease choose a different username."; 
      break; 
     case -2: 
      message = "Supplied email address has already been used."; 
      break; 
     default: 
      message = "Registration successful.\\nUser Id: " + userId.ToString(); 
      break; 
    } 
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true); 
} 

希望它可以幫助

+0

謝謝工作:D – rohillasarthak

+0

@rohillasarthak不客氣 – Khazratbek