Click here to Skip to main content
16,012,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string constr = @"Data Source=VISHAL;Initial Catalog=DrivingSchool;Integrated Security=True";
        SqlCommand cmd = new SqlCommand();
        private void btnSubmit_Click(object sender, EventArgs e)
        {


            using (SqlConnection cn = new SqlConnection(constr))
            {
                if (txtFName.Text == "" || txtLName.Text == "" || txtAge.Text == "" || cmbDate.Text == "" || cmbMonth.Text == "" || txtYear.Text == "" || cmbGender.Text == "" || txtAddress.Text == "" || txtContactno.Text == "" || cmbQualification.Text == "")
                {
                    MessageBox.Show("Fields Cannot be empty");

                }
                else
                {
                    MessageBox.Show("Employee Added Successfully");
                }
                try
                {
                    cn.Open();
                    cmd = new SqlCommand("insert into AddEmployee values('" + txtFName.Text + "','" + txtLName.Text + "','" + txtAge.Text + "','" + cmbGender.Text + "','" + cmbDate.Text + "','" + cmbMonth.Text + "','" + txtYear.Text + "','" + txtAddress.Text + "','" + txtContactno.Text + "','" + cmbQualification.Text + "')", cn);
                    cmd.ExecuteNonQuery();

                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);

                }
                finally
                {
                    cn.Close();
                }
            }
Posted
Comments
Arif Ansari Code 9-Nov-13 2:31am    
Please Enter your Error witch show on your system
vishal salve 9-Nov-13 2:37am    
no error or execption bt data also not inserted in database ...

1 solution

Hi Vishal,

You are just validating values in all the controls, if its not empty you are giving message "Employee added successfully" without even executing the query.

Try this code:
C#
string constr = @"Data Source=VISHAL;Initial Catalog=DrivingSchool;Integrated Security=True";
        SqlCommand cmd = new SqlCommand();
        private void btnSubmit_Click(object sender, EventArgs e)
        {


            using (SqlConnection cn = new SqlConnection(constr))
            {
                if (txtFName.Text == "" || txtLName.Text == "" || txtAge.Text == "" || cmbDate.Text == "" || cmbMonth.Text == "" || txtYear.Text == "" || cmbGender.Text == "" || txtAddress.Text == "" || txtContactno.Text == "" || cmbQualification.Text == "")
                {
                    MessageBox.Show("Fields Cannot be empty");

                }
                else
                {
                 try
                {
                    cn.Open();
                    cmd = new SqlCommand("insert into AddEmployee values('" + txtFName.Text + "','" + txtLName.Text + "','" + txtAge.Text + "','" + cmbGender.Text + "','" + cmbDate.Text + "','" + cmbMonth.Text + "','" + txtYear.Text + "','" + txtAddress.Text + "','" + txtContactno.Text + "','" + cmbQualification.Text + "')", cn);
                    int isInserted = cmd.ExecuteNonQuery();
                    if (isInserted == 1)
                    {
                    MessageBox.Show("Employee Added Successfully");
                    }
                    else
                    {
                    MessageBox.Show("Employee insertion failed");
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);

                }

                }

                finally
                {
                    cn.Close();
                }
            }


Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900