我们可以在C#中绑定多个dropdownblist的数据吗? [英] Can we bind the data for multiple dropdownblist in C#?

查看:67
本文介绍了我们可以在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();

}

}
}

解决方案

你必须告诉我们哪一行导致错误。



无论如何,试试这个:



  protected   void  Page_Load( object  sender,EventArgs e){
if (!IsPostback){
BindAll();
}
}

私人 void BindAll( ){
BindExpenseList();
BindPersonList();
}

private string GetConnectionString(){
return ConfigurationManager.ConnectionStrings [ conString]的ConnectionString;
}


private void BindExpenseList() {
使用(SqlConnection con = new SqlConnection(GetConnectionString())){
使用(SqlCommand cmd = new SqlCommand( select * from Expense_Type,con)){
con.Open();
DropDownListexpensetype.DataSource = cmd.ExecuteReader();
DropDownListexpensetype.DataTextField = Expense_Type;
DropDownListexpensetype.DataValueField = Expense_Type;
DropDownListexpensetype.DataBind();
}
}
}

private void BindPersonList(){
使用(SqlConnection con = new SqlConnection(GetConnectionString() )){
使用(SqlCommand cmd = new SqlCommand( select * from [dbo]。[User_details],con)){
con.Open();
DropDownListperson.DataSource = cmd.ExecuteReader();
DropDownListperson.DataTextField = Full_Name;
DropDownListperson.DataValueField = Full_Name;
DropDownListperson.DataBind();
}
}
}





想法是在单独的方法中解耦每个绑定您可以轻松维护代码。


https://www.c-sharpcorner.com/UploadFile/d1558d/working-with-multiple-dropdownlist-using-Asp-Net/

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();

        }

    }
}

解决方案

You have to tell us which line causes the error.

Anyway, try this instead:

protected void Page_Load(object sender, EventArgs e){
       if(!IsPostback){
	      BindAll();
       }  
	}
	
	private void BindAll(){
		BindExpenseList();
		BindPersonList();
	}
	
	private string GetConnectionString(){
		return ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
	}


	private void BindExpenseList(){
            using(SqlConnection con = new SqlConnection(GetConnectionString())){
            	using(SqlCommand cmd = new SqlCommand("select * from Expense_Type", con)){
			        con.Open();
            		DropDownListexpensetype.DataSource = cmd.ExecuteReader();
            		DropDownListexpensetype.DataTextField = "Expense_Type";
            		DropDownListexpensetype.DataValueField = "Expense_Type";
            		DropDownListexpensetype.DataBind();
	    	}
	    }
    }
	
	private void BindPersonList(){
            using(SqlConnection con = new SqlConnection(GetConnectionString())){
            	using(SqlCommand cmd = new SqlCommand("select * from [dbo].[User_details]", con)){
			        con.Open();
            		DropDownListperson.DataSource = cmd.ExecuteReader();
                    DropDownListperson.DataTextField = "Full_Name";
                    DropDownListperson.DataValueField = "Full_Name";
                    DropDownListperson.DataBind();
	    	}
	    }
    }



The idea is to decouple each binding in a separate method for you to easily maintain the code.


https://www.c-sharpcorner.com/UploadFile/d1558d/working-with-multiple-dropdownlist-using-Asp-Net/


这篇关于我们可以在C#中绑定多个dropdownblist的数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆