Hi indradeo,
Check this sample. now take its reference and correct your code.
HTML
<table>
<tr>
<td>User ID</td>
<td><asp:Label ID="lblUserID" Text="" runat="server" /></td>
</tr>
<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><asp:Button ID="btnRetrive" Text="Retrive Data" runat="server" OnClick="GetData" /></td>
<td><asp:Button ID="btnSave" Text="Save" runat="server" OnClick="Save_Click" /></td>
</tr>
</table>
<asp:GridView runat="server" ID="gvEmployees">
</asp:GridView>
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 Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindEmployees();
}
}
protected void GetData(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT (CONVERT(VARCHAR(10),taId)+CONVERT(VARCHAR(10),Empid)) AS 'UserID',FirstName,LastName,Designation FROM tblEmployees WHERE EmpID=@EmpID", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@EmpID", txtEmpID.Text.Trim());
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
txtName.Text = sdr["FirstName"].ToString();
txtLastName.Text = sdr["LastName"].ToString();
txtDesignation.Text = sdr["Designation"].ToString();
lblUserID.Text = sdr["UserID"].ToString();
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Record Not Found For the given EmployeeID : " + txtEmpID.Text.Trim() + "')", true);
}
con.Close();
}
}
}
protected void Save_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Empid FROM tblEmployees WHERE Empid=@EmpID", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@EmpID", txtEmpID.Text.Trim());
con.Open();
string empID = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (string.IsNullOrEmpty(empID))
{
InsertData(this.txtEmpID.Text.Trim(), this.txtName.Text.Trim(), this.txtLastName.Text.Trim(), txtDesignation.Text.Trim());
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('EmployeeID Already Exists : " + txtEmpID.Text.Trim() + "')", true);
}
txtEmpID.Text = string.Empty;
txtName.Text = string.Empty;
txtLastName.Text = string.Empty;
txtDesignation.Text = string.Empty;
}
}
this.BindEmployees();
}
private void InsertData(string EmpID, string name, string lastName, string designation)
{
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", EmpID);
cmd1.Parameters.AddWithValue("@FirstName", name);
cmd1.Parameters.AddWithValue("@LastName", lastName);
cmd1.Parameters.AddWithValue("@Designation", designation);
con.Open();
cmd1.ExecuteNonQuery();
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Record Saved Successfully for EmployeeID : " + txtEmpID.Text.Trim() + "')", true);
con.Close();
}
}
}
private void BindEmployees()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmpID,(CONVERT(VARCHAR(10),taId)+CONVERT(VARCHAR(10),Empid)) AS 'UserID',FirstName,LastName,Designation FROM tblEmployees", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvEmployees.DataSource = dt;
this.gvEmployees.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindEmployees()
End If
End Sub
Protected Sub GetData(ByVal sender As Object, ByVal e As EventArgs)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT (CONVERT(VARCHAR(10),taId)+CONVERT(VARCHAR(10),Empid)) AS 'UserID',FirstName,LastName,Designation FROM tblEmployees WHERE EmpID=@EmpID", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@EmpID", txtEmpID.Text.Trim())
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
txtName.Text = sdr("FirstName").ToString()
txtLastName.Text = sdr("LastName").ToString()
txtDesignation.Text = sdr("Designation").ToString()
lblUserID.Text = sdr("UserID").ToString()
Else
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Record Not Found For the given EmployeeID : " & txtEmpID.Text.Trim() & "')", True)
End If
con.Close()
End Using
End Using
End Sub
Protected Sub Save_Click(ByVal sender As Object, ByVal e As EventArgs)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT Empid FROM tblEmployees WHERE Empid=@EmpID", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@EmpID", txtEmpID.Text.Trim())
con.Open()
Dim empID As String = Convert.ToString(cmd.ExecuteScalar())
con.Close()
If String.IsNullOrEmpty(empID) Then
InsertData(Me.txtEmpID.Text.Trim(), Me.txtName.Text.Trim(), Me.txtLastName.Text.Trim(), txtDesignation.Text.Trim())
Else
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('EmployeeID Already Exists : " & txtEmpID.Text.Trim() & "')", True)
End If
txtEmpID.Text = String.Empty
txtName.Text = String.Empty
txtLastName.Text = String.Empty
txtDesignation.Text = String.Empty
End Using
End Using
Me.BindEmployees()
End Sub
Private Sub InsertData(ByVal EmpID As String, ByVal name As String, ByVal lastName As String, ByVal designation As String)
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", EmpID)
cmd1.Parameters.AddWithValue("@FirstName", name)
cmd1.Parameters.AddWithValue("@LastName", lastName)
cmd1.Parameters.AddWithValue("@Designation", designation)
con.Open()
cmd1.ExecuteNonQuery()
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Record Saved Successfully for EmployeeID : " & txtEmpID.Text.Trim() & "')", True)
con.Close()
End Using
End Using
End Sub
Private Sub BindEmployees()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmpID,(CONVERT(VARCHAR(10),taId)+CONVERT(VARCHAR(10),Empid)) AS 'UserID',FirstName,LastName,Designation FROM tblEmployees", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvEmployees.DataSource = dt
Me.gvEmployees.DataBind()
End Using
End Using
End Using
End Sub
Screenshot