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:
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