hi guys,

how to find the next primarykey value from a table b4 inserting?

using System;
using System.Data;
using System.Data.SqlClient;


string sConnectionString = "Integrated Security=SSPI;Initial Catalog=;Data Source=localhost;";


SqlConnection objConn = new SqlConnection(sConnectionString);
objConn.Open();


string sSQL = "SELECT MAX tableid FROM tablename";
SqlCommand objCmd = new SqlCommand(sSQL,objConn);
SqlDatareader  objDr;
objDr = objCmd.ExecuteReader();


objDr.read();
int nextid = objDr[0];


if (nextid == 0)
{
nextid = 1;
}
else if(nextid > 0 )
{
nextid += 1;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.