1

How can i read multiple column values into an array list? I am trying to read a list of category names and category ids from database into an array list; i am then binding these values into drop-down list. With my current code, i am able to do with one column only but would like to pull both cat_name and cat_id so how can i do that?

 <asp:DropDownList ID="DropDownList1" runat="server"  DataTextField="ct_name" DataValueField="ct_id" AppendDataBoundItems="true">
            <asp:ListItem Value="-1">Select</asp:ListItem>
            </asp:DropDownList>

here is code behind

 private ArrayList GetDummyData()
 {
     ArrayList arr = new ArrayList();
     string strConn = ConfigurationManager.ConnectionStrings["myCon"].ConnectionString.ToString();
     SqlConnection con = new SqlConnection(strConn);
     con.Open();
     SqlCommand cmd = new SqlCommand("select distinct ct_name, cat_id from [myTable].[dbo].[categories]", con);
     SqlDataReader objDR = cmd.ExecuteReader();

     if (objDR != null) {
         while (objDR.Read())
         {
             //fill arraylist
             arr.Add(objDR["ct_name"]);
         }
     }

     con.Close();
     return arr;
 }

 private void FillDropDownList(DropDownList ddl)
 {
     ArrayList arr = GetDummyData();
     foreach (string item in arr)
     {
         ddl.Items.Add(item);
     }
}    
4
  • maybe you should use a Dictionary<int,string>, dic.Add(Convert.ToInt32(objDR["cat_id"]),objDR["ct_name"]) Commented Jun 19, 2015 at 16:14
  • Side note: in most cases best use of multidimensional array (like int[10,10]) is not to... Also it does not look like you have even single array - ArrayList is not an "array" and generally obsolete (use List<T> instead). Commented Jun 19, 2015 at 16:15
  • Why don't you replace using the reader and just return a DataTable? You can bind the Rows to your DropDownList. Your DataTexField and DataValueField are set correctly already. Otherwise you should do ddl.Items.Add(new ListItem(objDR["ct_name"], objDR["ct_id"])); No need for a temporary list of objects. Commented Jun 19, 2015 at 16:22
  • Thanks Dacker, can you show me the example please? Commented Jun 19, 2015 at 16:28

2 Answers 2

3

Best way to use "multidimensional array" is commonly not to use one and instead use strongly typed data:

class Category
{
  public string Name {get;set;}
  public string Id {get;set;}
}

List<Category> categories = new List<Category>();
while (objDR.Read())
{
    categories.Add(new Category { 
       Name = objDR["ct_name"],
       Id =  objDR["ct_id"],
    };
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you're trying to put both items in the dropdownlist side by side, then you could use

SELECT concat(cat_id," ", ct_name) ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.