本文介绍了我们可以在C#中绑定多个dropdownblist的数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在webform中有3个下拉列表,所以我想从SQL数据库绑定数据,我能够为一个下拉列表执行此操作..如何从同一个sqldatabase绑定其他两个下拉列表的数据...
我的代码:
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.SqlClient;
使用System.Configuration;
命名空间Expense_System
{
公共部分类事务:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
String strConnString = ConfigurationManager.ConnectionStrings [conString]。ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand cmd = new SqlCommand(select * from Expense_Type,con);
DropDownListexpensetype.DataSource = cmd.ExecuteReader();
DropDownListexpensetype.DataTextField =Expense_Type;
DropDownListexpensetype.DataValueField =Expense_Type;
DropDownListexpensetype.DataBind();
}
protected void BtncreateExpense_Click(object sender,EventArgs e)
{
String strConnString = ConfigurationManager.ConnectionStrings [conString]。ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText =[dbo]。[Spcreatetransactions];
cmd.Parameters.Add(@ Payee,SqlDbType.VarChar).Value = tbpayee.Text;
cmd.Parameters.Add(@ Date,SqlDbType.Date).Value = tbdate.Text;
cmd.Parameters.Add(@ AMOUNT,SqlDbType.Int).Value = tbamount.Text;
cmd.Parameters.Add(@ expensetype,SqlDbType.VarChar).Value = DropDownListexpensetype.Text;
cmd.Parameters.Add(@ person,SqlDbType.Int).Value = DropDownListperson.Text;
cmd.Parameters.Add(@ card,SqlDbType.Int).Value = DropDownListcard.Text;
cmd.Connection = con;
int totalrecords =(int)cmd.ExecuteNonQuery();
Response.Write(创建的事务数为+ totalrecords.ToString());
con.Close();
}
}
}
什么我试过了:
i也尝试了这样但不起作用,它抛出了unhandledexception错误。
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.SqlClient;
使用System.Configuration;
命名空间Expense_System
{
公共部分类事务:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
String strConnString = ConfigurationManager.ConnectionStrings [conString]。ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand cmd = new SqlCommand(select * from Expense_Type,con);
DropDownListexpensetype.DataSource = cmd.ExecuteReader();
DropDownListexpensetype.DataTextField =Expense_Type;
DropDownListexpensetype.DataValueField =Expense_Type;
DropDownListexpensetype.DataBind();
SqlCommand cmd2 = new SqlCommand(select * from [dbo]。[User_details],con);
DropDownListperson.DataSource = cmd2.ExecuteReader();
DropDownListperson.DataTextField =Full_Name;
DropDownListperson.DataValueField =Full_Name;
DropDownListperson.DataBind();
}
protected void BtncreateExpense_Click(object sender,EventArgs e)
{
String strConnString = ConfigurationManager.ConnectionStrings [conString]。ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText =[dbo]。[Spcreatetransactions];
cmd.Parameters.Add(@ Payee,SqlDbType.VarChar).Value = tbpayee.Text;
cmd.Parameters.Add(@ Date,SqlDbType.Date).Value = tbdate.Text;
cmd.Parameters.Add(@ AMOUNT,SqlDbType.Int).Value = tbamount.Text;
cmd.Parameters.Add(@ expensetype,SqlDbType.VarChar).Value = DropDownListexpensetype.Text;
cmd.Parameters.Add(@ person,SqlDbType.Int).Value = DropDownListperson.Text;
cmd.Parameters.Add(@ card,SqlDbType.Int).Value = DropDownListcard.Text;
cmd.Connection = con;
int totalrecords =(int)cmd.ExecuteNonQuery();
Response.Write(创建的事务数为+ totalrecords.ToString());
con.Close();
}
}
}
解决方案
I am having 3 dropdownlist in a webform so i want to bind the data from SQL database, i am able to do it for one dropdownlist.. how to bind the data for other two dropdownlist from same sqldatabase...
my code:
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; namespace Expense_System { public partial class Transactions : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(strConnString); con.Open(); SqlCommand cmd = new SqlCommand("select * from Expense_Type",con); DropDownListexpensetype.DataSource = cmd.ExecuteReader(); DropDownListexpensetype.DataTextField = "Expense_Type"; DropDownListexpensetype.DataValueField = "Expense_Type"; DropDownListexpensetype.DataBind(); } protected void BtncreateExpense_Click(object sender, EventArgs e) { String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(strConnString); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "[dbo].[Spcreatetransactions]"; cmd.Parameters.Add("@Payee", SqlDbType.VarChar).Value = tbpayee.Text; cmd.Parameters.Add("@Date", SqlDbType.Date).Value = tbdate.Text; cmd.Parameters.Add("@AMOUNT", SqlDbType.Int).Value = tbamount.Text; cmd.Parameters.Add("@expensetype", SqlDbType.VarChar).Value = DropDownListexpensetype.Text; cmd.Parameters.Add("@person ", SqlDbType.Int).Value = DropDownListperson.Text; cmd.Parameters.Add("@card ", SqlDbType.Int).Value = DropDownListcard.Text; cmd.Connection = con; int totalrecords = (int)cmd.ExecuteNonQuery(); Response.Write("The number of created transactions is" + totalrecords.ToString()); con.Close(); } } }
What I have tried:
i have tried like this also but not working, it was throwing unhandledexception error.
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; namespace Expense_System { public partial class Transactions : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(strConnString); con.Open(); SqlCommand cmd = new SqlCommand("select * from Expense_Type",con); DropDownListexpensetype.DataSource = cmd.ExecuteReader(); DropDownListexpensetype.DataTextField = "Expense_Type"; DropDownListexpensetype.DataValueField = "Expense_Type"; DropDownListexpensetype.DataBind(); SqlCommand cmd2 = new SqlCommand("select * from [dbo].[User_details]", con); DropDownListperson.DataSource = cmd2.ExecuteReader(); DropDownListperson.DataTextField = "Full_Name"; DropDownListperson.DataValueField = "Full_Name"; DropDownListperson.DataBind(); } protected void BtncreateExpense_Click(object sender, EventArgs e) { String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(strConnString); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "[dbo].[Spcreatetransactions]"; cmd.Parameters.Add("@Payee", SqlDbType.VarChar).Value = tbpayee.Text; cmd.Parameters.Add("@Date", SqlDbType.Date).Value = tbdate.Text; cmd.Parameters.Add("@AMOUNT", SqlDbType.Int).Value = tbamount.Text; cmd.Parameters.Add("@expensetype", SqlDbType.VarChar).Value = DropDownListexpensetype.Text; cmd.Parameters.Add("@person ", SqlDbType.Int).Value = DropDownListperson.Text; cmd.Parameters.Add("@card ", SqlDbType.Int).Value = DropDownListcard.Text; cmd.Connection = con; int totalrecords = (int)cmd.ExecuteNonQuery(); Response.Write("The number of created transactions is" + totalrecords.ToString()); con.Close(); } } }
解决方案
这篇关于我们可以在C#中绑定多个dropdownblist的数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!