I keep getting this exception but I don't know why
Procedure or function 'InsertNewUser' expects parameter '@Username', which was not supplied
Any ideas?
try
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "InsertNewUser";
cmd.Parameters.Add(parameterName: "@Username", sqlDbType: SqlDbType.VarChar).Value = UsernameTextBox.Text.ToString();
cmd.Parameters.Add(parameterName: "@Email", sqlDbType: SqlDbType.VarChar).Value = EmailTextBox.Text.ToString();
cmd.Parameters.Add(parameterName: "@Password", sqlDbType: SqlDbType.VarChar).Value = PasswordTextBox.Text.ToString();
cmd.Parameters.Add(parameterName: "@FirstName", sqlDbType: SqlDbType.VarChar).Value = FirstNameTextBox.Text.ToString();
cmd.Parameters.Add("@SecondName", SqlDbType.VarChar).Value = SecondNameTextbox.Text.ToString();
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Registration Successful");
}
catch(Exception ex)
{
MessageBox.Show("Registration Failed");
}
This will work:
try
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "InsertNewUser";
cmd.CommandType = CommandType.StoredProcedure; //<--- Specify your command type
cmd.Parameters.AddWithValue("@Username", UsernameTextBox.Text);
cmd.Parameters.AddWithValue("@Email", EmailTextBox.Text);
cmd.Parameters.AddWithValue("@Password", PasswordTextBox.Text);
cmd.Parameters.AddWithValue("@FirstName", FirstNameTextBox.Text);
cmd.Parameters.AddWithValue("@SecondName", SecondNameTextbox.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Registration Successful");
}
catch(Exception ex)
{
MessageBox.Show("Registration Failed");
}