how to generate automatic employee ids in sqlserver using c#

Posted by reddysankark-13471 under Sql Server on 3/3/2012 | Points: 10 | Views : 23839 | Status : [Member] | Replies : 11
hi
how to generate employee no in companies.by using asp.net,c# 3.5,sqlserver 2005

sankarreddy


Responses

Posted by: Akiii on: 3/3/2012 [Member] Bronze | Points: 25

Up
0
Down
Try generating a random number of integer. If you do want to generate other than integer than you can also do that !
And then store that in the table along with the primary key...


Regards
Akiii


reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 3/3/2012 [Member] Bronze | Points: 25

Up
0
Down
Here are two links that will help you :-

http://www.dotnetperls.com/random

http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber10272005160549PM/RandomNumber.aspx


Thanks and Regards
Akiii

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kk86kiran on: 3/4/2012 [Member] Starter | Points: 25

Up
0
Down
when ur creating employee table u can use identity function. Identity function will automatically generate number.

for example:

create table emp(ename varchar(20),eno int identity,sal int)


k.m.j.kiran

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 3/5/2012 [Member] Starter | Points: 25

Up
0
Down
Keep the Column autogenrate means make it primary Key .It will Hold a only one value for every employee.

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriramnandha on: 5/20/2012 [Member] Starter | Points: 25

Up
0
Down
U CAN USE IDENTITY COLUMN TO AUTOMATIC GENERATE EMPLOYEE TABLE...


CREATE TABLE EMPLOYEE(EMPID INT IDENITY(1,1),EMPNAME VARCHAR(90))

REGARDS

sriram

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jone_Rit on: 6/25/2012 [Member] Starter | Points: 25

Up
0
Down
public string generate_holidayid()
{
try
{
string holidayid;
if (conn.State == ConnectionState.Closed)
conn.Open();

cmd = new SqlCommand("holiday_mast", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@HOLIDAY_ID","JP"));
cmd.Parameters.Add(new SqlParameter("@HOLIDAY_NAME", "JP"));
cmd.Parameters.Add(new SqlParameter("@HOLIDAY_DESC", "JP"));
cmd.Parameters.Add(new SqlParameter("@HOLIDAY_FROMDATE", "12/12/12"));
cmd.Parameters.Add(new SqlParameter("@HOLIDAY_TODATE", "12/12/12"));
cmd.Parameters.Add(new SqlParameter("@mode", 6));
DataSet dtset = new DataSet();

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(dtset);
conn.Close();

if (dtset.Tables[0].Rows.Count <= 0)
{
holidayid = "HOL" + 01;
return holidayid;
}
else
{
char[] ch = { 'H', 'O', 'L'};
int id = int.Parse(dtset.Tables[0].Rows[0].ItemArray[0].ToString().Trim(ch)) + 1;
holidayid = "HOL" + id;
return holidayid;
}
}
catch (Exception e)
{
return null;
}
}

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/25/2012 [Member] Silver | Points: 25

Up
0
Down
Make employee id column as primary key auto increment.
Google SQL server auto increment to see how it can be possible.

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/25/2012 [Member] Silver | Points: 25

Up
0
Down
Also tell us,What have you done so far and where do you stuck?


reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 6/26/2012 [Member] Bronze | Points: 25

Up
0
Down
CREATE TABLE TABLE NAME(COLUMN1 INT IDENTITY(1,1) PRIMARY KEY NOT NULL,COLUMN2 VARCHAR(10))

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

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

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="EmpId" Font-Bold="True" Width="100px"></asp:Label>
<asp:TextBox ID="txt_empid" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="EmpName" Font-Bold="True" Width="100px"></asp:Label>
<asp:TextBox ID="txt_empname" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="Salary" Font-Bold="True" Width="100px"></asp:Label>
<asp:TextBox ID="txt_sal" runat="server"></asp:TextBox><br />
</div>

<asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"
Text="Insert Data" /><br />
<asp:Label ID="Label4" runat="server"></asp:Label>


</form>
</body>
</html>


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

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kundan64 on: 2/3/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Reddysankark,
If you want to generate the Employee id automatically then you can do it in SqlServer itself, no need to write a Single line of code in C#.
If you want to see your Employee id like 1, 2, 3, 4 ....... then use AutoIncrement Column.
If you want to see your Employee id looks like xyz001, xyz002...... then you can use Computed Column for this type of Employee id.

Thanks

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response