Here enclosed my code :
private void btnAutoID_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection("Data Source=sakthi\\sqlexpress; Initial Catalog=LoginDetails; Integrated Security=True;");
SqlCommand sqlcmd = new SqlCommand("select isnull(max(empcode), 0) from emp", sqlcon);
{
sqlcon.Open();
object obj = sqlcmd.ExecuteScalar();
sqlcon.Close();
txtEmpCode.Text = (int.Parse(obj.ToString()) + 1).ToString();
}
}

Jignesh TrivediRank #16 · 50.3K reputationPosted Oct 16, 2013, 1:47 AM EDT
try
private void btnAutoID_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection("Data Source=sakthi\\sqlexpress; Initial Catalog=LoginDetails; Integrated Security=True;");
SqlCommand sqlcmd = new SqlCommand("select isnull(max(Cast(empcode as int)), 0) + 1 from emp", sqlcon);
{
sqlcon.Open();
object obj = sqlcmd.ExecuteScalar();
sqlcon.Close();
txtEmpCode.Text = obj.ToString();
}
}
hope this will help you.
Jignesh TrivediRank #16 · 50.3K reputationPosted Oct 15, 2013, 11:55 PM EDT
your code look ok. this can also achieve with in query.....
try
private void btnAutoID_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection("Data Source=sakthi\\sqlexpress; Initial Catalog=LoginDetails; Integrated Security=True;");
SqlCommand sqlcmd = new SqlCommand("select isnull(max(empcode), 0) + 1 from emp", sqlcon);
{
sqlcon.Open();
object obj = sqlcmd.ExecuteScalar();
sqlcon.Close();
txtEmpCode.Text = obj.ToString();
}
}
hope this will help you.
Sakthis50 reputationPosted Oct 16, 2013, 1:32 AM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Oct 16, 2013, 1:36 AM EDT
Sakthis50 reputationPosted Oct 16, 2013, 1:45 AM EDT
Sakthis50 reputationPosted Oct 16, 2013, 1:53 AM EDT