【发布时间】:2014-04-20 22:18:51
【问题描述】:
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using System.Web.Security;
using System.IO;
public partial class Search : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlCommand cmd1;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillEmpDropdownList();
}
}
protected void FillEmpDropdownList()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["database1ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adp = new SqlDataAdapter();
DataTable dt = new DataTable();
try
{
cmd = new SqlCommand("Select * from Emp_Tb", con);
adp.SelectCommand = cmd;
adp.Fill(dt);
ddlEmpRecord.DataSource = dt;
ddlEmpRecord.DataTextField = "Emp_Id";
ddlEmpRecord.DataValueField = "Emp_Id";
ddlEmpRecord.DataBind();
ddlEmpRecord.Items.Insert(0, "-- Select --");
//ddlEmpRecord.Items.Insert(0, new ListItem("Select Emp Id", "-1"));
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
}
finally
{
cmd.Dispose();
adp.Dispose();
dt.Clear();
dt.Dispose();
}
}
protected void ddlEmpRecord_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int empId = Convert.ToInt32(ddlEmpRecord.SelectedValue);
BindEmpGrid(empId);
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
}
}
private void BindEmpGrid(Int32 empId)
{
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
SqlCommand cmd1 = new SqlCommand("select * from Emp_Tb where Emp_Id=" + empId + " ", con);
adp.SelectCommand = cmd1;
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
grdEmp.DataSource = dt;
lblEmpId.Text = "Emp Id :" + dt.Rows[0]["Emp_Id"].ToString();
lblEmpName.Text ="Emp Name: " + dt.Rows[0]["EmpName"].ToString();
lblCity.Text = "City: " +dt.Rows[0]["City"].ToString();
lblSalary.Text = "Salary: " + dt.Rows[0]["Salary"].ToString();
grdEmp.DataBind();
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
}
finally
{
dt.Clear();
dt.Dispose();
adp.Dispose();
}
}
}
【问题讨论】:
-
究竟在哪一行?
标签: c#