Solved

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

Posted on 2007-03-23
6
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

696 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