0

I have made an sql query and i am trying to use it to store registrations into a database but the error message of "could not find stored procedure" keeps coming up i am just wondering if there is something key I am missing.

  public partial class Register : Form
    {
        string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|Login12.mdf;Integrated Security=True;Asynchronous Processing=True;User Instance=False;Context Connection=False";
        public Register()
        {
            InitializeComponent();
        }

        private void BtnRegistered_Click(object sender, EventArgs e)
        {
            var myForm = new LOGIN();
            myForm.Show();
            Close();
            if (UNtxtBox.Text == "" || PasswordtxtBox.Text == "")
                MessageBox.Show("Please Fill Madatory Fields");
            using (SqlConnection sqlCon = new SqlConnection(connectionString))
            {
                sqlCon.Open();
                SqlCommand sqlCmd = new SqlCommand("UserAdd",sqlCon);
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@MNumber",IDtxtBox.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Username", UNtxtBox.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Password", PasswordtxtBox.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@FirstName", FNtxtBox.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Surname", SNtxtBox.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Email", EAtxtBox.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@DOB", DOBtxtBox.Text.Trim());
                sqlCmd.ExecuteNonQuery();
                MessageBox.Show("Success");
                Clear();
            }

This is my SQL Query:

CREATE PROCEDURE UserAdd
@Mnumber nvarchar(50),
@Username nvarchar(50),
@Password nvarchar(50),
@FirstName nvarchar(50),
@Surname nvarchar(50),
@Email nvarchar(50),
@DOB date
AS
    INSERT INTO ABCD(Username,Password,FirstName,Surname,Email,DOB)
    VALUES(@Username,@Password,@FirstName,@Surname,@Email,@DOB)
danrutley
  • 19
  • 3

1 Answers1

0

Database connection and whether the stored procedure exists aside, I've never had much luck with AddValue it can be a little ambiguous as to data types.

I would suggest that you move away from directly implementing Text box data passes into SQL parameter passes. Handling the data from your UI separately allows you to run sanity checks on the values passed to your stored procedure.

Try building your SqlParameter before passing as then you can stipulate their data types to match the table e.g.,

    sqlCmd.Parameters.Add(new SqlParameter("@Mnumber", SqlDbType.NVarChar, 50) {Value = "Some text"});

Also, I see that you are passing Text to your @DOB parameter, even though it is declared as a date type in your table.

sqlCmd.Parameters.AddWithValue("@DOB", DOBtxtBox.Text.Trim())

but declared in the table as

@DOB date
ChrisBD
  • 9,104
  • 3
  • 22
  • 35