I had problem that how to return auto incremented id of table when record is inserted in table using C# and MS Access database. The following is the code that I tried.
string data1 = string.Format("insert into Enquiry(fname,mname,lname,date_of_enq,address,contact_no,email_id,course_id,other,probable_date,updated_date,status)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')",
txtfName.Text, txtmName.Text, txtlName.Text, fullDate.Text, txtaddress.Text, txtContactNo.Text, varemail, CBCourse.SelectedValue.ToString(), txtOther.Text, DTPFeedBackDate.Text, updated_date, status);
I want the id of that inserted record which is auto incremented in table. How can I get it?
just add eg
SELECT @@IDENTITYto your query, and execute this as a scalar (be aware, that there are other ways, depending on the scope of your identity)this applies to mssql – for other systems i am awaiting your answer on my comment!
btw: i would not execute a query in this way – the keyword is sql-injection. rather go for parameters!