Solved

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

Posted on 2007-03-23
6
730 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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