Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not getting value of Id from table

I am using following code :
C#
DataTable dt = new DataTable();
if (dt.Rows.Count > 0)
  txtLinkID.Text = (Convert.ToInt32(dt.Rows[0]["link_id"]) + 1).ToString();
else
  txtLinkID.Text = "1";

So tell me how to get value of database (i.e. ID increment value) in textbox in ASP.NET
Posted
Updated 10-Jul-14 0:28am
v2
Comments
arvind mepani 10-Jul-14 6:22am    
Your code is right , Provide structure of your table for further explanation.

1 solution

Don't.

Don't even think about it.
Why not? Because most databases are multi-user systems, so you can't rely on the "next" value being one greater that the current last value: all it takes is one other user to be doing the same thing, and the whole system falls apart like a house of cards.

Only ever try to get the value of an automatic variable after the data which causes it to change has been written. Any attempt to do it beforehand leave you wide open to some horrible, horrible problems which are extremely difficult to sort out afterwards - particularly if you use the data at any point and that causes bad values to be inserted in the DB. Working out which should be there and which belong over there after the fact is a total nightmare, and wastes huge amounts of time.
 
Share this answer
 
Comments
adriancs 12-Jul-14 9:45am    
or insert an empty row and return that ID for that specific row.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900