Hi AnkitPal,
Please refer below sample code.
Code
public partial class Form1 : Form
{
SqlConnection cnn = new SqlConnection(@"Server=.\SQL2005;DataBase=Test;UID=user;PWD=password");
public Form1()
{
InitializeComponent();
DataRow dr;
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[] { new DataColumn("Value", typeof(string)), new DataColumn("Name", typeof(string)) });
dt1.Rows.Add("C01", "Chhibramau");
dt1.Rows.Add("B01", "Bidhuna");
dt1.Rows.Add("K01", "Kannauj");
dr = dt1.NewRow();
dr[0] = 0;
dr[1] = "-Please Select-";
dt1.Rows.InsertAt(dr, 0);
comboBox1.DataSource = dt1;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Value";
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[] { new DataColumn("Value", typeof(string)), new DataColumn("Name", typeof(string)) });
dt2.Rows.Add("AD", "Adviser");
dt2.Rows.Add("DO", "Development Officer");
dt2.Rows.Add("UN", "Unit");
dr = dt2.NewRow();
dr[0] = 0;
dr[1] = "-Please Select-";
dt2.Rows.InsertAt(dr, 0);
comboBox2.DataSource = dt2;
comboBox2.DisplayMember = "Name";
comboBox2.ValueMember = "Value";
DataTable dt3 = new DataTable();
cnn.Open();
SqlDataAdapter sda = new SqlDataAdapter("Select * From UniqueIdUsingDDL", cnn);
sda.Fill(dt3);
dataGridView1.DataSource = dt3;
cnn.Close();
}
private string UniqueId(string connection, string query)
{
string NewId = string.Empty;
string BrCode = Convert.ToString(comboBox1.SelectedValue.ToString());
string PostCode = Convert.ToString(comboBox2.SelectedValue.ToString());
using (SqlConnection cnn = new SqlConnection(connection))
{
cnn.Open();
SqlCommand cmd = new SqlCommand(query, cnn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string i = dr[0].ToString();
if (string.IsNullOrEmpty(i))
{
NewId = BrCode + PostCode + "000001"; //NEMP ( NEW Employee)
}
else
{
i = i.Substring(5);
int j = Convert.ToInt32(i);
j = j + 1;
NewId = BrCode + PostCode + j.ToString().PadLeft(5, '0');
}
}
cnn.Close();
}
return string.Concat(NewId);
}
private void button1_Click(object sender, EventArgs e)
{
string avoidrepeatid = @"Server=.\SQL2005;DataBase=Test;UID=user;PWD=password";
string previousId = "SELECT MAX(EmpCode) FROM UniqueIdUsingDDL WHERE BrCode='" + comboBox1.SelectedValue + "' and Post='" + comboBox2.SelectedValue + "'";
string EmpCode = UniqueId(avoidrepeatid, previousId);
if (comboBox1.Text == "-Please Select-" || comboBox2.Text == "-Please Select-" || textBox1.Text == "")
{
MessageBox.Show("Please Enter Name and Select Branch Code and Employee Post.");
}
else
{
SqlCommand cmd = new SqlCommand("Select * From UniqueIdUsingDDL where EmpName='" + textBox1.Text + "'", cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i > 0)
{
MessageBox.Show("This Employee Name - '" + textBox1.Text + "'\nis Already Axists.");
ds.Clear();
}
else
{
try
{
cnn.Open();
SqlCommand cmdinsert = new SqlCommand("Insert into UniqueIdUsingDDL Values('" + textBox1.Text + "','" + comboBox1.SelectedValue.ToString() + "','" + comboBox1.Text.Trim() + "','" + comboBox2.SelectedValue.ToString() + "','" + comboBox2.Text.Trim() + "','" + EmpCode + "')", cnn);
cmdinsert.ExecuteNonQuery();
cnn.Close();
SqlDataAdapter sda = new SqlDataAdapter("Select * From UniqueIdUsingDDL", cnn);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
MessageBox.Show("Employee Code has been Created Successfully.\nNew Employee Code is - '" + EmpCode + "'");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}