Data Base Table Name : Employee
empid varchar(50) Checked
empname varchar(50) Checked
sal int Checked
.aspx Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="EmpId" Font-Bold="True"
Width="100px"></asp:Label>
<asp:TextBox ID="txt_empid" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="EmpName" Font-Bold="True" Width="100px"></asp:Label>
<asp:TextBox ID="txt_empname" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="Salary" Font-Bold="True" Width="100px"></asp:Label>
<asp:TextBox ID="txt_sal" runat="server"></asp:TextBox><br />
</div>
<asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"
Text="Insert Data" /><br />
<asp:Label ID="Label4" runat="server"></asp:Label>
</div>
<div>
<p>Select Employee ID</p>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
<br /><br /><br />
<hr />
<br />
Employee Name: <asp:TextBox ID="txtCompanyName" runat="server"></asp:TextBox>
<br />
Salary: <asp:TextBox ID="txtContactTitle" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
.cs Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
int count;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
str = "select count(*) from Employee";
com = new SqlCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
txt_empid.Text = "E000" + count;
con.Close();
if (!IsPostBack)
{
bindDropdown();
}
}
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "insert into Employee values('" + txt_empid.Text.Trim() + "','" + txt_empname.Text.Trim() + "'," + txt_sal.Text.Trim() + ")";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Label4.Text = "Records successfully Inserted";
txt_empid.Text = "";
txt_empname.Text = "";
txt_sal.Text = "";
}
public void bindDropdown()
{
DropDownList1.Items.Add(new ListItem("Select Employee", ""));
DropDownList1.AppendDataBoundItems = true;
String strQuery = "SELECT empid, empname FROM Employee";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "empname";
DropDownList1.DataValueField = "empid";
DropDownList1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
String strQuery = "SELECT * FROM Employee WHERE empid = @empid";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@empid", DropDownList1.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
txtCompanyName.Text = sdr["empname"].ToString();
txtContactTitle.Text = sdr["sal"].ToString();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
No comments:
Post a Comment