-4
string conStr = null;
SqlCommand cmd;
SqlConnection cnn;
string sql = null;

conStr = "Data Source=DELL-PC\\SQLEXPRESS;Initial  Catalog=DBMSI;Integrated Security=True";

sql = "insert into CEC_Employee values('"+empid + "','" + name + "','" + fname + "','" + mname + "','" + lname + "','" + address + "','" + postcode + "','" + job + "','" + sdate + "','" + whours + "','" + sph + "','" + spa + "','" + location + "','" + working + "','" + gender + "','" + dob + "','" + pn + "','" + exp + "','" + vtype + "','" + vexp + "','" + qualification + "','" + email + "','" + number + "','" + nin + "','" + sort + "','" + acc + "','" + bank + "','" + nname + "','" + rel + "','" + addkin + "','" + cnokin + "','" + emailkin + "')";

cnn = new SqlConnection(conStr);

try
{
    cnn.Open();
    cnn = new SqlConnection(conStr);
    cmd = new SqlCommand(sql, cnn);

    cmd.ExecuteNonQuery();

    cmd.Dispose();

    cnn.Open();

    MessageBox.Show("Employee Details registered Succesffuly");
    // Keeps on moving to the Exception part of the code. Doesn't execute the try portion of the program. 
}
catch (Exception ex)
{
    MessageBox.Show("Error Occoured - Employee Details were not recorded");
}

Found the code online. Please help to make it work. Thanks!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Omesh
  • 45
  • 1
  • 1
  • 7

1 Answers1

0

Hopefully your primary key on CEC_Employee isn't "empid", and if it is set to be an autonumber, like IDENTITY(1,1), the SQL command will fail as it won't let you hand it a primary key value.

This is speculation of course, since you haven't posted the actual exception message or stack trace.

gpratt
  • 1
  • 1
  • 1
    This "answer" is somewhere between an answer and a comment (with a bit more rep, [you will be able to post comments](http://stackoverflow.com/privileges/comment)). Please consider either elaborating and formatting some more to make it a proper answer, or delete this (or have someone convert it into a comment). – ryanyuyu Apr 03 '15 at 19:45