Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();

        }

    }
}
Posted
Updated 19-Jul-18 14:52pm
Comments
Simon_Whale 19-Jul-18 6:23am    
what is the exception message?

You have to tell us which line causes the error.

Anyway, try this instead:

C#
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.
 
Share this answer
 
https://www.c-sharpcorner.com/UploadFile/d1558d/working-with-multiple-dropdownlist-using-Asp-Net/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900