I have the following code to create a new record in SQL but I don't know how to get the autonumber for the new record.
I have searched google and here for hours and have tried many different ways but nothing seems to be a good fit for the way my code is.
Please, I don't want to rewrite my code to use a procedure. I just want to get it from what i have, otherwise the only other thing i can think of is to execute another query WHERE device serial number and name = the new record. I havent done that yet case that seems so inefficient.
Thanks in advance for your replies.
Code:
SqlCommand cmd = new SqlCommand("INSERT INTO device (" +
"device_name,device_serial_num,device_created,device_created_by)" +
" VALUES (@name, @serialnum, @created, @createdby)", con);
cmd.Parameters.Add("@name", SqlDbType.NChar, 15).Value = txt_deviceName.Text.ToString();
cmd.Parameters.Add("@serialnum", SqlDbType.NChar, 10).Value = txt_deviceSerialNumber.Text.ToString();
cmd.Parameters.Add("@created", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@createdby", SqlDbType.NChar, 10).Value = strUser;
cmd.ExecuteNonQuery();
GetDeviceDetails( {put newly created ID here} );
I have searched google and here for hours and have tried many different ways but nothing seems to be a good fit for the way my code is.
Please, I don't want to rewrite my code to use a procedure. I just want to get it from what i have, otherwise the only other thing i can think of is to execute another query WHERE device serial number and name = the new record. I havent done that yet case that seems so inefficient.
Thanks in advance for your replies.