2014-12-01 43 views
-1

我是絕對的初學者,我解決不了的問題,收到錯誤&例外是在必須在Insert語句聲明標量變量「@UserId」

objCommand.ExecuteNonQuery();

異常消息拋出:「 必須聲明標量變量@userId。

  private void btnRegister_Click(object sender, EventArgs e) 
    { 
     if (tbxUserName.Text == "" || tbxPassword.Text == "") 
     { 
      MessageBox.Show("Please enter values!"); 
      return; 
     } 
     string strConnection; 
     strConnection = ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString; 
     SqlConnection objConnection = new SqlConnection(); 
     objConnection.ConnectionString = strConnection; 
     objConnection.Open(); 

     string strSQL; 
     SqlCommand objCommand = new SqlCommand("SELECT * FROM LoginTable WHERE UserId='" + tbxUserName.Text + "';", objConnection); 
     SqlDataAdapter objAdapter = new SqlDataAdapter(); 
     objAdapter.SelectCommand = objCommand; 
     objAdapter.Fill(objDataSet); 

     int i = objDataSet.Tables[0].Rows.Count; 

     if (i > 0) 
     { 
      MessageBox.Show("User Name " + tbxUserName.Text + " already exists"); 
      tbxPassword.Text = ""; 
      objDataSet.Clear(); 
     } 

     else 
     { 
      strSQL = "INSERT INTO LoginTable(UserId, Password) VALUES(@UserId, @Password)";   
      objCommand.Parameters.AddWithValue("@UsesrId", tbxUserName.Text); 
      objCommand.Parameters.AddWithValue("@Password", tbxPassword.Text); 

      objCommand.CommandText = strSQL; 
      objCommand.ExecuteNonQuery(); 
      objConnection.Close(); 

      message = "Registered Successfully! " + "Welcome " + tbxUserName.Text; 
      this.Hide(); 
      WelcomeForm wf = new WelcomeForm(message); 
      wf.Show(); 

     } 
    } 

回答

0

您在代碼中有錯字,將其更改爲以下。(@UsesrId!= @UserId)

strSQL = "INSERT INTO LoginTable(UserId, Password) VALUES(@UserId, @Password)";   
     objCommand.Parameters.AddWithValue("@UserId", tbxUserName.Text); 
+0

只是近距離投票這樣的問題。 – 2014-12-01 19:32:53

+0

謝謝Atilla Ozgur – 2014-12-01 20:01:30