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 TrivediPosted Oct 16, 2013, 5:47 AM
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.
SakthisPosted Oct 16, 2013, 5:53 AM
SakthisPosted Oct 16, 2013, 5:45 AM
Jignesh TrivediPosted Oct 16, 2013, 5:36 AM
SakthisPosted Oct 16, 2013, 5:32 AM
Jignesh TrivediPosted Oct 16, 2013, 3:55 AM
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.