Hey AnkitPal,
Please refer below sample.
Namespaces
C#
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
public Form1()
{
InitializeComponent();
lblNewCId.Visible = false;
}
string connStr = @"Server=.\SQL2005;DataBase=Test;UID=sa;PWD=pass@123";
private void button1_Click(object sender, EventArgs e)
{
string previousCidQuery = "SELECT MAX(UniqueId) UniqueId FROM GenerateUniqueId";
string newCID = GenerateNewCID(connStr, previousCidQuery);
lblNewCId.Text = newCID.ToString();
string insertQuery = "INSERT INTO GenerateUniqueId VALUES('" + newCID + "')";
InsertNewCID(connStr, insertQuery);
}
private void InsertNewCID(string connection, string query)
{
using (SqlConnection con = new SqlConnection(connection))
{
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
private string GenerateNewCID(string connection, string query)
{
lblNewCId.Visible = true;
string newCID = string.Empty;
using (SqlConnection con = new SqlConnection(connection))
{
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string i = dr[0].ToString();
if (string.IsNullOrEmpty(i))
{
newCID = "GP" + DateTime.Now.ToString("yyMM") + ddlCSHD.Text + "1".PadLeft(6, '0');
}
else
{
i = i.Substring(10);
int j = Convert.ToInt32(i);
j = j + 1;
newCID = "GP" + DateTime.Now.ToString("yyMM") + ddlCSHD.Text + j.ToString().PadLeft(6, '0'); ;
}
}
con.Close();
}
return newCID;
}
VB.Net
Public Sub New()
InitializeComponent()
lblNewCId.Visible = False
End Sub
Private connStr As String = "Server=.\SQL2005;DataBase=Test;UID=sa;PWD=pass@123"
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim previousCidQuery As String = "SELECT MAX(UniqueId) UniqueId FROM GenerateUniqueId"
Dim newCID As String = GenerateNewCID(connStr, previousCidQuery)
lblNewCId.Text = newCID.ToString()
Dim insertQuery As String = "INSERT INTO GenerateUniqueId VALUES('" & newCID & "')"
InsertNewCID(connStr, insertQuery)
End Sub
Private Sub InsertNewCID(ByVal connection As String, ByVal query As String)
Using con As SqlConnection = New SqlConnection(connection)
con.Open()
Dim cmd As SqlCommand = New SqlCommand(query, con)
cmd.ExecuteNonQuery()
con.Close()
End Using
End Sub
Private Function GenerateNewCID(ByVal connection As String, ByVal query As String) As String
lblNewCId.Visible = True
Dim newCID As String = String.Empty
Using con As SqlConnection = New SqlConnection(connection)
con.Open()
Dim cmd As SqlCommand = New SqlCommand(query, con)
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
Dim i As String = dr(0).ToString()
If String.IsNullOrEmpty(i) Then
newCID = "GP" & DateTime.Now.ToString("yyMM") + ddlCSHD.Text & "1".PadLeft(6, "0"c)
Else
i = i.Substring(10)
Dim j As Integer = Convert.ToInt32(i)
j = j + 1
newCID = "GP" & DateTime.Now.ToString("yyMM") + ddlCSHD.Text & j.ToString().PadLeft(6, "0"c)
End If
End While
con.Close()
End Using
Return newCID
End Function
Screenshot