-3

I tried this code, and without combobox it's working.

After adding combobox(comboBloodGroup) with 8 selected items and data type selected is nvchar(50), I get an error

How to code to insert combobox data into sql database.

My new code is

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {         
            string strConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\USERS\MYPC\DOCUMENTS\LOGIN.MDF";
            SqlConnection cn = new SqlConnection(strConnectionString);
            cn.Open();
            int iId = Convert.ToInt32(txtId.Text.Trim());
            string strFName = txtFName.Text.Trim();
            string strLName = txtLName.Text.Trim();
            int iAge = Convert.ToInt32(txtAge.Text.Trim());
            string stremail = txtemail.Text.Trim();
            string strMobile = txtMobile.Text.Trim();
            string strAddress = txtAddress.Text.Trim();
            string strCity = txtCity.Text.Trim();
            int iPinCode = Convert.ToInt32(txtPinCode.Text.Trim());
            string strGender;
            if (rbMale.Checked)
                strGender = "Male";
            else
                strGender = "Female";
            string strBloodGroup = comboBloodGroup.SelectedItem.ToString();                
            DateTime dtDOB = dtPickerDOB.Value;
            string query = "INSERT INTO information(Id, FName, LName, Age, email, Mobile, Address, City, PinCode, Gender, BloodGroup, DOB)VALUES(@iId, @strFName, @strLName, @iAge, @stremail,@strMobile, @strAddress, @strCity, @iPinCode, @strGender, @strBloodGrourp, @dtDOB)";
            SqlCommand InsertCommand = new SqlCommand(query, cn);
            InsertCommand.Connection = cn;
            InsertCommand.Parameters.AddWithValue("@iId", iId);
            InsertCommand.Parameters.AddWithValue("@strFName", strFName);
            InsertCommand.Parameters.AddWithValue("@strLName", strLName);
            InsertCommand.Parameters.AddWithValue("@iAge", iAge);
            InsertCommand.Parameters.AddWithValue("@stremail", stremail);
            InsertCommand.Parameters.AddWithValue("@strMobile", strMobile);
            InsertCommand.Parameters.AddWithValue("@strAddress", strAddress);
            InsertCommand.Parameters.AddWithValue("@strCity", strCity);
            InsertCommand.Parameters.AddWithValue("@iPinCode", iPinCode);
            InsertCommand.Parameters.AddWithValue("@strGender", strGender);
            InsertCommand.Parameters.AddWithValue("@strBloodGroup", strBloodGroup);
            InsertCommand.Parameters.AddWithValue("@dtDOB", dtDOB);
            InsertCommand.ExecuteNonQuery();             
            MessageBox.Show("New Patient's Data has been added successfully");
            cn.Close();
        }
        catch (Exception ex)
        {
           MessageBox.Show(ex.Message);
        }
    }
2
  • 1
    What is the error exactly? On which line? Can you please be more specific about your problem? Aug 17, 2015 at 7:20
  • the error is "must declare the scalar variable '@strBloodGroup'."
    – harini
    Aug 17, 2015 at 7:28

3 Answers 3

0

You mistyped @strBloodGrourp in query. Change to @strBloodGroup.

string query = "INSERT INTO information(Id, FName, LName, Age, email, Mobile, Address, City, PinCode, Gender, BloodGroup, DOB)VALUES(@iId, @strFName, @strLName, @iAge, @stremail,@strMobile, @strAddress, @strCity, @iPinCode, @strGender, **@strBloodGrourp**, @dtDOB)";

And you add parameter @strBloodGroup

0
0

You have a typo

string query = "INSERT INTO information(Id, FName, LName, Age, email, Mobile, Address, City, PinCode, Gender, BloodGroup, DOB)VALUES(@iId, @strFName, @strLName, @iAge, @stremail,@strMobile, @strAddress, @strCity, @iPinCode, @strGender, @strBloodGrourp, @dtDOB)";

Should read

string query = "INSERT INTO information(Id, FName, LName, Age, email, Mobile, Address, City, PinCode, Gender, BloodGroup, DOB)VALUES(@iId, @strFName, @strLName, @iAge, @stremail,@strMobile, @strAddress, @strCity, @iPinCode, @strGender, @strBloodGroup, @dtDOB)";

You had an extra R in bloodgroup

0
0

Your parameter name you defined in your command and your AddWithValue doesn't match. You defined it as strBloodGrourp in your command but you declared it strBloodGroup in your AddWithValue.

Change your parameter name in your command as;

@strGender, @strBloodGroup, @dtDOB

A few things more;


using(var cn = new SqlConnection(strConnectionString))
using(var InsertCommand = cn.CreateCommand())
{
    // Set your CommandText property with parameter names.
    // Define your parameter names and it's values. Add them to your command.
    // Open your connection.
    // Execute your query.
}
1
  • 1
    If this solved your problem please mark it as an answer Aug 17, 2015 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.