how create empids using C#

Posted by reddysankark-13471 under C# on 7/30/2012 | Points: 10 | Views : 2276 | Status : [Member] | Replies : 7
hi,

can any body tell me how create empids like EMP001 type using c# and asp.net while registration

in the same manner how to create empid in sql server 2005 also

please give me these two answers

sankarreddy


Responses

Posted by: Mcamkumaran on: 7/31/2012 [Member] Starter | Points: 25

Up
0
Down
You want to add the employee ids like EMP001,EMP002 ...etc
First you have to store the EMP string as constant then whenever you add the employee that time you want to generate the employee id like EMP001 so that you have to get the last employee id from the sql server then you have to the +1 to the employee id you are getting new id then you have to save it.

Example
public const string EmployeePrefix = "EMP";
select the last employee id from sql server for example EMP005
select the last 3 charector from the string you got like 005
then convert the string to int and add one you will get 6 then convert the int to char that time you have to check the whether the string as single digit or 2 digit or 3 digit if single digid you have to 2 zero before the string like 006 then already we have const EMP then you will get finally EMP006 then save it to database.



MuthuKumaran.R
Senior Software Engineer
Techcello
India.

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

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

Up
0
Down
Please see this...

DECLARE @employeecount INT


DECLARE @empid VARCHAR(10)

DECLARE @tempid INT

SELECT @employeecount= COUNT(*) FROM emp WHERE emp_id LIKE'EMP%'

IF @employeecount=0

SELECT @empid='EMP001';

ELSE

BEGIN

SELECT @tempid =MAX(CAST(REPLACE(emp_id,'EMP','')AS INT )+1 ) FROM emp;

IF LEN(@tempid)=1

SELECT @empid='EMP00'+CAST(@tempid AS VARCHAR(10));

IF LEN(@tempid)=2

SELECT @empid='EMP0'+CAST(@tempid AS VARCHAR(10));

IF LEN(@tempid)>2

SELECT @empid='EMP'+CAST(@tempid AS VARCHAR(10));

END

PRINT @empid;


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

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

Up
0
Down
For more details please see this link,
http://www.dotnetfunda.com/codes/code2630-generating-employee-code-like-emp007-in-sqlserver2008.aspx

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

Posted by: reddysankark-13471 on: 7/31/2012 [Member] Starter | Points: 25

Up
0
Down
hi

i can understand some what could you please give me coding for this

sankarreddy

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

Posted by: Mcamkumaran on: 7/31/2012 [Member] Starter | Points: 25

Up
0
Down
public const string EmployeePrefix="EMP";

For Sql Server

Select Top(1) EmployeeId from Employees OrderBy EmployeeId Desc

here you are getting EMP010

Then
string returnEmployeeid = from query(EMP010);
returnemployeeId = returnemployeeid.substring(3,5).ToString();

so you got 010 the convertt into int
int count = convert.toint32(returnemployeeid);
count=count+1;
if(count.length >=3)
{
returnemployeeid = EmployeePrefix + count;
}
else if(count.length == 2)
{
returnemployeeid = EmployeePrefix + "0" + count;
}
else
{
returnemployeeid = EmployeePrefix + "00" + count;
}

then save the returnemployeeid to database for a new record.

MuthuKumaran.R
Senior Software Engineer
Techcello
India.

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

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

Up
0
Down
Mark as answer if it helps...,That motivates...!!!

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

Posted by: Satyapriyanayak on: 1/24/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Dynamic_generate_id
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;
int count;

public Form1()
{
InitializeComponent();
}

private void btn_save_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "insert into caste(cast_code,cast_desc) values('" + lbl_castecode.Text.Trim() + "','" + txt_castename.Text.Trim() + "')";
com = new OleDbCommand(str, con);
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Records successfully Inserted");
txt_castename.Text = "";
autogenerated();
}
private void autogenerated()
{
OleDbConnection con = new OleDbConnection(ConnectionString);
str = "select count(*) from caste";
com = new OleDbCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
lbl_castecode.Text = "EMP00" + count.ToString();
lbl_castecode.Enabled = false;
con.Close();
}

private void Form1_Load(object sender, EventArgs e)
{
autogenerated();
}
}
}


If this post helps you mark it as answer
Thanks

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

Login to post response