绑定下拉列表的最佳方法是什么
问题描述:
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;
namespace WebApplication1
{
public partial class Details : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ABCConnectionString2"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * from emp_details";
cmd.Connection = conn;
conn.Open();
// SqlDataAdapter ds = new SqlDataAdapter("Select * from emp_details ",conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
//DataTable dt = new DataTable();
//ds.Fill(dt);
DropDownList1.DataSource = dt;
// DropDownList1.DataSource = dt.Tables[0];
DropDownList1.DataTextField = "emp_name";
DropDownList1.DataValueField = "emp_Id";
DropDownList1.DataBind();
//conn.Close();
}
}
protected void Button_submit_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ABCConnectionString2"].ConnectionString);
conn.Open();
string InsertQuery = "insert into emp_details set(emp_name,father_name,age,sex,dob,nationality,state,city,mobile_no,exp,qual,emp_sal,email) values(@name,@fname,@age,@sex,@dob,@nation,@state,@city,@mob,@exp,@qual,@sal,@email) ";
SqlCommand com = new SqlCommand(InsertQuery, conn);
com.Parameters.AddWithValue("@name", TextBox_emp_name.Text);
com.Parameters.AddWithValue("fname", TextBox_father_name.Text);
com.Parameters.AddWithValue("@age", TextBox_Age.Text);
com.Parameters.AddWithValue("@sex", RadioButtonList_sex.Text);
com.Parameters.AddWithValue("@dob", TextBox_dob.Text);
com.Parameters.AddWithValue("@nation", TextBox_nationality.Text);
com.Parameters.AddWithValue("@state", TextBox_state.Text);
com.Parameters.AddWithValue("@city", TextBox_city.Text);
com.Parameters.AddWithValue("@mob", TextBox_mob.Text);
com.Parameters.AddWithValue("@exp", DropDownList_exp.Text);
com.Parameters.AddWithValue("@qual", DropDownList_qual.Text);
com.Parameters.AddWithValue("@sal", TextBox_sal.Text);
com.Parameters.AddWithValue("@email", TextBox_email.Text);
com.ExecuteNonQuery();
conn.Close();
Response.Write("Insertion Succesfull");
}
protected void Button_update_Click(object sender, EventArgs e)
{
conn.Open();
string updateQuery = "update emp_details(emp_name,father_name,age,sex,dob,nationality,state,city,mobile_no,exp,qual,emp_sal,email) values(@name,@fname,@age,@sex,@dob,@nation,@state,@city,@mob,@exp,@qual,@sal,@email) ";
SqlCommand com = new SqlCommand(updateQuery, conn);
com.Parameters.AddWithValue("@name", TextBox_emp_name.Text);
com.Parameters.AddWithValue("fname", TextBox_father_name.Text);
com.Parameters.AddWithValue("@age", TextBox_Age.Text);
com.Parameters.AddWithValue("@sex", RadioButtonList_sex.Text);
com.Parameters.AddWithValue("@dob", TextBox_dob.Text);
com.Parameters.AddWithValue("@nation", TextBox_nationality.Text);
com.Parameters.AddWithValue("@state", TextBox_state.Text);
com.Parameters.AddWithValue("@city", TextBox_city.Text);
com.Parameters.AddWithValue("@mob", TextBox_mob.Text);
com.Parameters.AddWithValue("@exp", DropDownList_exp.Text);
com.Parameters.AddWithValue("@qual", DropDownList_qual.Text);
com.Parameters.AddWithValue("@sal", TextBox_sal.Text);
com.Parameters.AddWithValue("@email", TextBox_email.Text);
com.ExecuteNonQuery();
conn.Close();
Response.Write("Updation Succesfull");
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataAdapter ds = new SqlDataAdapter("Select * from emp_deatils where emp_name='"+DropDownList1.Text+"' ", conn);
DataTable dt = new DataTable();
ds.Fill(dt);
TextBox_father_name.Text = dt.Rows[0][1].ToString();
TextBox_Age.Text = dt.Rows[0][2].ToString();
RadioButtonList_sex.Text = dt.Rows[0][3].ToString();
TextBox_dob.Text = dt.Rows[0][4].ToString();
TextBox_nationality.Text = dt.Rows[0][5].ToString();
TextBox_state.Text = dt.Rows[0][6].ToString();
TextBox_city.Text = dt.Rows[0][7].ToString();
TextBox_mob.Text = dt.Rows[0][8].ToString();
DropDownList_exp.Text = dt.Rows[0][9].ToString();
DropDownList_qual.Text = dt.Rows[0][10].ToString();
TextBox_sal.Text = dt.Rows[0][11].ToString();
TextBox_email.Text = dt.Rows[0][12].ToString();
DropDownList1.DataBind();
}
protected void Button_delete_Click(object sender, EventArgs e)
{
}
答
尝试
将数据绑定到数据库中的下拉列表在asp net [ ^ ]
使用c#将数据绑定到下拉列表 [ ^ ]
向数据库中的Asp.net下拉列表显示绑定数据C#,VB.NET [ ^ ]
Try
Bind data to drop down list from database in asp net [^]
bind data to dropdownlist with c#[^]
Show Bind Data to Asp.net Dropdownlist from Database in C#, VB.NET [^]
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter ds = new SqlDataAdapter();
DataTable dt = new DataTable();
cmd.Connection = conn;
cmd.CommandText =select * from emp_details;
ds.SelectCommand = cmd;
ds.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1。 DataTextField =emp_name;
DropDownList1.DataValueField =emp_Id;
DropDownList1.DataBind();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter ds = new SqlDataAdapter();
DataTable dt = new DataTable();
cmd.Connection = conn;
cmd.CommandText = "Select * from emp_details";
ds.SelectCommand=cmd;
ds.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "emp_name";
DropDownList1.DataValueField = "emp_Id";
DropDownList1.DataBind();
}
}