sun
sun

Reputation: 85

How to combine database table field values and load to a dropdownlist?

This is my database table. Now I want to combine Branch_Id, Acc_no and Acc_Type like 000178963503 and 000211111102 and display it in a dropdownlist

This is my database table. Now I want to combine Branch_Id, Acc_no and Acc_Type so that it looks like 000178963503 or 000211111102 and display it in a dropdownlist My code is

string[] account_number;
 string account;
for (int j = 0; j < dtAcc.Rows.Count;j++)
                    {
                        account = Convert.ToString(dtAcc.Rows[j][2])+Convert.ToString(dtAcc.Rows[j][5]) + Convert.ToString(dtAcc.Rows[j][3]);   ///Account Number
                        account_number =account.Split(' ');                                              
                    }
Drp_AccType.DataSource = account_number;
                    Drp_AccType.DataBind();

Thanks

Upvotes: 0

Views: 278

Answers (3)

Keagan Ladds
Keagan Ladds

Reputation: 458

First of all, you cant assign directly to 'account_number' because its an array, you will have to use the index ie.

account_number[j] = account;

Also you have to initialize your array to the amount of items it will hold because if you try access an item that is not in the array you will get an 'Access Violation Error'

I would rather use this method, you don't need to use an array then;

string account;
Drp_AccType.Items.Clear(); //This removes previous items because you are not using    
//DataBind method
for (int j = 0; j < dtAcc.Rows.Count;j++)
                {
                    account = Convert.ToString(dtAcc.Rows[j][2])+Convert.ToString(dtAcc.Rows[j][5]) + Convert.ToString(dtAcc.Rows[j][3]);   ///Account Number
                    Drp_AccType.Items.Add(account); //Add directly to
 //dropdown box                                 
                }

Upvotes: 0

Arjun Shetty
Arjun Shetty

Reputation: 1585

You can concat string when selecting from the database in query

Eg: select Branch_Id + Acc_no + Acc_Type from tablename as col1

in code :

DataSet    ds=GetDataSet("select Branch_Id + Acc_no + Acc_Type from tablename as col1");
Drp_AccType.DataSource = ds;
Drp_AccType.DataTextField="col1"
Drp_AccType.DataValueField="col1"
                    Drp_AccType.DataBind();

Upvotes: 0

Haris Hasan
Haris Hasan

Reputation: 30097

I am not sure what are you achieving with account_number =account.Split(' ');

//here is the list which will be source of combo box
List<String> accounts = new List<String>();
//go through each row
for (int j = 0; j < dtAcc.Rows.Count;j++)
{
    //combine branch, account no and type
    String account = Convert.ToString(dtAcc.Rows[j]["Branch_Id"]) + Convert.ToString(dtAcc.Rows[j]["Acc_no"]) + Convert.ToString(dtAcc.Rows[j]["Acc_Type"]);   ///Account Number
    //add account/row record to list
    accounts.Add(account);                                              
}
//bind combo box
Drp_AccType.DataSource = accounts;
Drp_AccType.DataBind();

Upvotes: 2

Related Questions