Solved

How to loop through Sqldatareader and populate list box without binding?

Posted on 2007-03-23
6
710 Views
Last Modified: 2012-05-05
Using Asp.net I need to populate a listbox at runtime with values from a datareader.  I can't bind, because I will be moving values out of that list box (or maybe I could bind and unbind at run time?) Here is my existing code:
protected void PopulateListBox()
    {

        string myconnstr = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
        SqlCommand objCmdFun;
        SqlDataReader drfun;

        try
        {
           
            objCnFun = new SqlConnection(myconnstr);
            objCnFun.Open();
            objCmdFun = new SqlCommand("FunctionList",objCnFun);
            objCmdFun.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.Add(new SqlParameter("@siteid", myvar);
            drfun = objCmd.ExecuteReader(CommandBehavior.SequentialAccess);
            while (drfun.Read())
            {
                //add items to listbox here, with field 0 of rowset being value, and field 1 being text
            }
           

        }

        catch (Exception e)

        {
            string errormsg = e.Message.ToString();
        }

        finally
        {
            objCnFun.Close();

        }

           
           
           
           


       




    }



}
   
0
Comment
Question by:vhpcomp
6 Comments
 
LVL 43

Assisted Solution

by:TimCottee
TimCottee earned 125 total points
ID: 18780456
while (drfun.read())
{
  MyListBox.Items.Add(drfun.item(0).value,drfun.item(1).value);
}

Should be all you need to do.
0
 
LVL 22

Assisted Solution

by:_TAD_
_TAD_ earned 125 total points
ID: 18780477

Your code looks fine.  What problem are you having?

while (drfun.Read())
            {
                listbox1.Items.Add(drfun[1].Value, drfun[0].Value);  // Column 0 has a name, column 1 has the value
            }


The only catch is that if this is an ASP.Net application, you have to run this code during the page_load process, otherwise the endusers will not see it.
0
 
LVL 8

Expert Comment

by:gelbert
ID: 18780501
You can create temporary data source (DataTable for example), replace your while() loop with following

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
while(true == drfun.Read() )
{
   DataRow dr = dt.NewRow();
   dr[0] = drfun.GetString();
   dt.Rows.Add(dr);
}
  YourDataList.DataSource = new DataView(dt);

where YourDataList is yout DataList type  of object
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:vhpcomp
ID: 18781076
Tad,
Your code

listbox1.Items.Add(drfun[1].Value, drfun[0].Value);

looks like the C# version  that I am looking for, but when I try that line I get:
object does not contain a definition for 'Value'
So, I looked at some of my previous interactions with a datareader and tried:
listbox1.Items.Add(drfun[1].ToString(), drfun[0].ToString());
I then get 'No overload for method add takes 2 arguments'

I'm sure it's something simple I'm overlooking....
0
 
LVL 8

Accepted Solution

by:
gelbert earned 250 total points
ID: 18781246
if you want ot pass 2 parameters than you have to use following version of Add()
public void Add(   ListItem item )
0
 
LVL 1

Author Comment

by:vhpcomp
ID: 18781451
Ok , I split points because everyone's response helped in figuring out.
I ended up with
ListItem myListItem = newListIItem();
myListItem.Value = drfun[0].ToString();
myListItem.Text = drfun[1].ToString(;
Listbox1.Items.Add(myListItem)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now