Generating unique incremental integer ID for database tables

Posted by Anilkumar431 under C# on 2/22/2013 | Points: 10 | Views : 1585 | Status : [Member] | Replies : 3
hi all,
how to Generating unique incremental integer ID for database tables in C# class.
please give me class solution.
thanx in advance...




Responses

Posted by: Satyapriyanayak on: 2/22/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
int count;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
autogenerated();

}
}

void autogenerated()
{
SqlConnection con = new SqlConnection(strConnString);
str = "select count(*) from employee";
com = new SqlCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
txt_empid.Text = count.ToString();
txt_empid.Enabled = false;
con.Close();
}
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "insert into employee 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";
txt_empname.Text = "";
txt_sal.Text = "";
autogenerated();
}
}


If this post helps you mark it as answer
Thanks

Anilkumar431, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anilkumar431 on: 2/22/2013 [Member] Starter | Points: 25

Up
0
Down
Thanx a lot man..

Anilkumar431, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 2/22/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
If this post helps you mark it as answer
Thanks

If this post helps you mark it as answer
Thanks

Anilkumar431, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response