-1

I have created a web page(Using ASP.NET,c#) which contains a Registration Form. And also have a SQL Server DB. In the registration_form.aspx page, the fields are (User ID, Name, Add., Mob No,........... etc.) and a SUBMIT button. When i'm pressing the submit button after filling up those fields, the data is storing into the DB. But the problem is that, User ID is not increasing automatically into the DB and aren't retrieving the next user id in the User ID field of web page. Pls help me on this matter.

Thanking You.

ARIT
  • 1
  • 4

1 Answers1

1

Make sure your id field in the database is set to identity

Tjaart van der Walt
  • 5,149
  • 2
  • 30
  • 50
  • Yes, i've done this changes. Now User ID increasing automatically by 1 by 1 into the DB. But again when the registration_form.aspx page is loading, the Next User ID(Let, previous user ID was 1001, and next User ID will be 1002) is not retrieving into the user id field in the web page, still now whose details is not entered. – ARIT Apr 12 '14 at 08:56
  • Are you trying to retrieve the new id after user has registered? – Tjaart van der Walt Apr 12 '14 at 10:31
  • Are you adding the record to sql with your own insert statement or entity framework? – Tjaart van der Walt Apr 12 '14 at 19:03
  • Please share your code you are using, the sql side and the asp.net side – Tjaart van der Walt Apr 12 '14 at 19:08
  • You can juat paste the code here, just show the part where you are calling the sql statement, and paste the sql code aswell – Tjaart van der Walt Apr 12 '14 at 19:17
  • This is not the correct way to get the latest id, you need to return the identity after your insert statement. – Tjaart van der Walt Apr 12 '14 at 19:37
  • void autogenerated() { SqlConnection con = Class.mycon(); String sql = "SELECT empid FROM Employee ORDER BY empid DESC";SqlCommand com = new SqlCommand(sql,con); con.Open(); } protected void btn_insert_Click(object sender, EventArgs e) { SqlConnection con1 = Class.mycon(); con1.Open();String sql1 = "insert into Employee values('" + TextBox2.Text.Trim() + "'," + TextBox3.Text.Trim() + ")"; SqlCommand com = new SqlCommand(sql1, con1); com.ExecuteNonQuery(); con1.Close(); TextBox2.Text = ""; TextBox3.Text = ""; autogenerated(); – ARIT Apr 12 '14 at 19:42
  • Pls give me the suggestion, how to write the code?? – ARIT Apr 12 '14 at 19:53
  • Firstly inline sql is a bad idea, a user can use a sql injection to cause harm to your database. Rather use a stored procedure to insert the record, and in the stored procedure return the identity of the new record. Just google using stored procedures to insert a record and return identity – Tjaart van der Walt Apr 12 '14 at 19:57
  • Check out this question http://stackoverflow.com/questions/4315628/return-identity-of-last-inserted-row-from-stored-procedure – Tjaart van der Walt Apr 12 '14 at 20:05