Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-03-23
6
Medium Priority
?
743 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 500 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 500 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 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