Hi indradeo,
Check this sample.
HTML
<table>
<tr>
<td>Emp ID</td>
<td><asp:TextBox ID="txtEmpID" runat="server" /></td>
</tr>
<tr>
<td>First Name</td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><asp:TextBox ID="txtLastName" runat="server" /></td>
</tr>
<tr>
<td>Designation</td>
<td><asp:TextBox ID="txtDesignation" runat="server" /></td>
</tr>
<tr>
<td colspan="2" align="center"><asp:Button ID="btnSave" Text="Save" runat="server" OnClick="OnSave" /></td>
</tr>
</table>
<asp:Label ID="lblUserID" Text="" runat="server" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void OnSave(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd1 = new SqlCommand("INSERT INTO tblEmployees VALUES(@Empid,@FirstName,@LastName,@Designation)", con))
{
cmd1.CommandType = CommandType.Text;
cmd1.Parameters.AddWithValue("@Empid", txtEmpID.Text.Trim());
cmd1.Parameters.AddWithValue("@FirstName", txtName.Text.Trim());
cmd1.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());
cmd1.Parameters.AddWithValue("@Designation", txtDesignation.Text.Trim());
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
string userID = this.GenerateID("SELECT (CONVERT(VARCHAR(10),taId)+CONVERT(VARCHAR(10),Empid)) AS 'UserID',FirstName,LastName,Designation FROM tblEmployees WHERE EmpID=" + txtEmpID.Text.Trim() + "");
lblUserID.Text = "Taid with join of two column " + userID;
txtEmpID.Text = string.Empty;
txtName.Text = string.Empty;
txtLastName.Text = string.Empty;
txtDesignation.Text = string.Empty;
}
}
}
private string GenerateID(string query)
{
string userID = "";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
userID = sdr["UserID"].ToString();
}
con.Close();
}
}
return userID;
}
VB.Net
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd1 As SqlCommand = New SqlCommand("INSERT INTO tblEmployees VALUES(@Empid,@FirstName,@LastName,@Designation)", con)
cmd1.CommandType = CommandType.Text
cmd1.Parameters.AddWithValue("@Empid", txtEmpID.Text.Trim())
cmd1.Parameters.AddWithValue("@FirstName", txtName.Text.Trim())
cmd1.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim())
cmd1.Parameters.AddWithValue("@Designation", txtDesignation.Text.Trim())
con.Open()
cmd1.ExecuteNonQuery()
con.Close()
Dim userID As String = Me.GenerateID("SELECT (CONVERT(VARCHAR(10),taId)+CONVERT(VARCHAR(10),Empid)) AS 'UserID',FirstName,LastName,Designation FROM tblEmployees WHERE EmpID=" & txtEmpID.Text.Trim() & "")
lblUserID.Text = "Taid with join of two column " & userID
txtEmpID.Text = String.Empty
txtName.Text = String.Empty
txtLastName.Text = String.Empty
txtDesignation.Text = String.Empty
End Using
End Using
End Sub
Private Function GenerateID(ByVal query As String) As String
Dim userID As String = ""
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
userID = sdr("UserID").ToString()
End If
con.Close()
End Using
End Using
Return userID
End Function
Screenshot