Could some one help me to resolve this error please, I have been trying without luck. I am trying to register user details in database using C# Professional 2013. I ma getting this error: An unhandled exception of the type 'System.Data.SqlClient.SqlException' occurred in system.Data.dll Additional information: Must declare the Scalar variable "@8".
Here is my code: I also seem not to save the image at all.
private void btnRegister_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "")
{
MessageBox.Show("Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUserName.Focus();
return;
}
if (cmbUserType.Text == "")
{
MessageBox.Show("Please select user type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUserName.Focus();
return;
}
if (txtPassword.Text == "")
{
MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPassword.Focus();
return;
}
if (txtConfirmPassword.Text != txtPassword.Text)
{
MessageBox.Show("The password does not match", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtConfirmPassword.Focus();
return;
}
if (txtFirstName.Text == "")
{
MessageBox.Show("Please enter name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtFirstName.Focus();
return;
}
if (txtPhoneNo.Text == "")
{
MessageBox.Show("Please enter contact no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPhoneNo.Focus();
return;
}
if (txtEmail.Text == "")
{
MessageBox.Show("Please enter email", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtEmail.Focus();
return;
}
con = new SqlConnection(cs.DBConn);
con.Open();
string ct = "select username from Registration where Username=@find";
cmd = new SqlCommand(ct);
cmd.Connection = con;
cmd.Parameters.Add(new SqlParameter("@find", System.Data.SqlDbType.VarChar, 30, "Username"));
cmd.Parameters["@find"].Value = txtUserName.Text;
rdr = cmd.ExecuteReader();
if (rdr.Read())
{
MessageBox.Show("Username Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUserName.Text = "";
txtUserName.Focus();
if ((rdr != null))
{
rdr.Close();
}
return;
}
con = new SqlConnection(cs.DBConn);
con.Open();
string cb = "insert into Registration(Username,Userpassword,Name,Surname,PhoneNumber,Email,DateOfBirth,UserType,Picture) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@8,@9)";
cmd = new SqlCommand(cb);
cmd.Connection = con;
cmd.Parameters.Add("@d1", System.Data.SqlDbType.VarChar);
cmd.Parameters.Add("@d2", System.Data.SqlDbType.VarChar);
cmd.Parameters.Add("@d3", System.Data.SqlDbType.VarChar);
cmd.Parameters.Add("@d4", System.Data.SqlDbType.VarChar);
cmd.Parameters.Add("@d5", System.Data.SqlDbType.VarChar);
cmd.Parameters.Add("@d6", System.Data.SqlDbType.VarChar);
cmd.Parameters.Add("@d7", System.Data.SqlDbType.VarChar);
cmd.Parameters.Add("@d8", System.Data.SqlDbType.VarChar);
MemoryStream MemStream = new MemoryStream();
Byte[] DataPic_Update = null;
this.pictureImage.Image.Save(MemStream, ImageFormat.Jpeg);
DataPic_Update = MemStream.GetBuffer();
MemStream.Read(DataPic_Update, 0, DataPic_Update.Length);
cmd.Parameters["@d1"].Value = txtUserName.Text.Trim();
cmd.Parameters["@d2"].Value = txtPassword.Text;
cmd.Parameters["@d3"].Value = txtFirstName.Text;
cmd.Parameters["@d4"].Value = txtLastName.Text;
cmd.Parameters["@d5"].Value = txtPhoneNo.Text;
cmd.Parameters["@d6"].Value = txtEmail.Text;
cmd.Parameters["@d7"].Value = dateDOB.Value;
cmd.Parameters["@d8"].Value = cmbUserType.Text;
SqlParameter photo = new SqlParameter("@d9", SqlDbType.Image);
photo.Value = DataPic_Update;
cmd.Parameters.Add(photo);
cmd.ExecuteReader();
con.Close();
con = new SqlConnection(cs.DBConn);
con.Open();
string cb1 = "insert into users(Username,Userpassword,UserType) VALUES (@d1,@d2,@d3)";
cmd = new SqlCommand(cb1);
cmd.Connection = con;
cmd.Parameters.Add(new SqlParameter("@d1", System.Data.SqlDbType.VarChar, 30, "Username"));
cmd.Parameters.Add(new SqlParameter("@d2", System.Data.SqlDbType.VarChar, 30, "Userpassword"));
cmd.Parameters.Add(new SqlParameter("@d3", System.Data.SqlDbType.VarChar, 15, "UserType"));
cmd.Parameters["@d1"].Value = txtUserName.Text.Trim();
cmd.Parameters["@d2"].Value = txtPassword.Text;
cmd.Parameters["@d3"].Value = cmbUserType.Text;
cmd.ExecuteReader();
con.Close();
MessageBox.Show("Successfully Registered", "User", MessageBoxButtons.OK, MessageBoxIcon.Information);
Autocomplete();
btnRegister.Enabled = false;
}
Thank you very much