Hi,
I want to generate 'ID' automatically and then store it in table please help me..
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Karthikeyan AnbarasanRank #71 · 25.5K reputationPosted Mar 17, 2011, 9:52 PM EDT
http://www.mjtnet.com/blog/2010/01/26/get-auto-generated-id-created-by-insert-sql/
Krishna GaradRank #191 · 12.8K reputationPosted Mar 17, 2011, 9:56 PM EDT
Declare @id int
Set @id=(Select Max(IdColumn+1) From TableName);
or
Set Column as Identity
Amruta Kirdat72 reputationPosted Mar 17, 2011, 11:59 PM EDT
//*** First create separte funtionc ***
public string GetNextId()
{
int NextId = 0;
try
{
OleDbConnection objCon = new OleDbConnection(strConn);
string strQuery = "select max(Column_Name) from TableName";
objCon.Open();
OleDbCommand objComm = new OleDbCommand(strQuery, objCon);
OleDbDataReader dr = objComm.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
if (dr.IsDBNull(0))
{
NextId = 0;
}
else
{
NextId = Convert.ToInt32(dr.GetValue(0));
}
}
dr.Close();
objCon.Close();
}
catch (Exception ex)
{
strFunction = "GetNextId()";
exInnerException = Convert.ToString(ex.InnerException);
errorLog.WriteToErrorLog(strClassName, strFunction, ex.Message, exInnerException);
}
return Convert.ToString(NextId + 1);
}
//****** call above function where you are wrinting query to insert into table like:
string strInsertQuery = "Insert Into [TableName] values(" + GetNextId + ")";
i hope this will help u