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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amruta KirdatPosted Mar 18, 2011, 3:59 AM
//*** 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
Krishna GaradPosted Mar 18, 2011, 1:56 AM
Declare @id int
Set @id=(Select Max(IdColumn+1) From TableName);
or
Set Column as Identity
Karthikeyan AnbarasanPosted Mar 18, 2011, 1:52 AM
http://www.mjtnet.com/blog/2010/01/26/get-auto-generated-id-created-by-insert-sql/