sandeep sandy

sandeep sandy

  • NA
  • 18
  • 6.7k

auto generate the id after data sucessfully inserted

May 11 2016 7:35 AM
HI,
the id should auto incremented after the data inserted into database,
here in my code after page refresh id is increasing can u modify my code and send it,
thanks in advance
 
----------------------------------------------------------------------------------------------
namespace Help_Desk
{
public partial class WebForm2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
//string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
int count;
protected void Page_Load(object sender, EventArgs e)
{
//SqlConnection con = new SqlConnection(strConnString);
str = "select count(*) from employee1";
com = new SqlCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
txt_empid.Text = "E00" + count;
con.Close();
}
protected void btn_insert_Click(object sender, EventArgs e)
{
//SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "insert into employee1 values('" + txt_empid.Text.Trim() + "','" + txt_empname.Text.Trim() + "'," + txt_sal.Text.Trim() + ")";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Label4.Text = "Records successfully Inserted";
}
}
}
------------------------------------------------------------------------------------------------------------------------ 

Answers (2)