Try This,
public void Autogenrate()
{
int r;
try
{
con = new SqlConnection("data source=XYZ; user id=ABC; password=***; initial catalog=Detail");
con.Open();
SqlCommand cmd = new SqlCommand("Select max(id) from empp1", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string d = dr[0].ToString();
if (d == "")
{
id.Text = "1001";
}
else
{
r = Convert.ToInt32(dr[0].ToString());
r = r + 1;
id.Text = r.ToString();
}
}
con.Close();
con.Open();
cmd.CommandText = "insert empp1 values('" + id.Text + "','" + txtName.Text + "','" + txtCity.Text + "')";
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write("<script>alert("+ex.Message+")</script>");
}
finally
{
con.Close();
}
}