Results 1 to 20 of 20

Thread: Can the DataReader bind to DropDownList control ?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Posts
    11

    Can the DataReader bind to DropDownList control ?

    Hi all,
    I need help on my ASP.NET Web Application.
    Can the DataReader bind to DropDownList control ?
    The following code give System.Data.Common.DBDataRecord error.

    Dim cnn As New OleDb.OleDbConnection(strConnTeamPE)
    cnn.Open()
    strSQL = "SELECT sTeamID FROM TeamMaster"
    Dim cmd As New OleDb.OleDbCommand(strSQL, cnn)
    Dim dr As OleDb.OleDbDataReader
    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    ' Bind Data Reader to DropDownList
    ddlTeamName.DataSource = dr
    ddlTeamName.DataBind()

    'Do While dr.Read
    ' ddlTeamName.Items.Add(dr("sTeamID"))
    'Loop

    If I replace last 2 lines with Do..While Loop then it's work.
    So, I wonder is DataReader able for DataBind method ?
    Many books does not teach binding controls with DataReader.(They used Do..While Loop or DataSet method)
    But according to 4GuysFromRolla.com, it can be done. (Pls see the acticles below)
    http://aspnet.4guysfromrolla.com/art...32702-1.2.aspx

    Please advice.

    Thanks.

    Regards,
    Hean

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You dont need the do while loop. Once you set the ddlTeamName datasource and databind, thats all you need to do. You would only use the do while loop if you were'nt databinding.
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Posts
    11
    This is what I did, but it give me give System.Data.Common.DBDataRecord error.
    Any ideas ?
    Thanks.

    Regards,
    Hean

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'm almost positive you can't bind to a datareader, because its a forward only kind of quick reader, you'll need to use a dataset if you want to bind.

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    In a Web App, and dataReader can be bound to a Web Form, it cant be bound to a WinForms App though. DataReaders work different in Web Apps and WinForms Apps.
    Dont gain the world and lose your soul

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Posts
    11
    Yes I Bind the DataReader in the Web Form, but it give me error.

    Below is my code, anything goes wrong ?

    The following code give System.Data.Common.DBDataRecord error.

    Dim cnn As New OleDb.OleDbConnection(strConnTeamPE)
    cnn.Open()
    strSQL = "SELECT sTeamID FROM TeamMaster"
    Dim cmd As New OleDb.OleDbCommand(strSQL, cnn)
    Dim dr As OleDb.OleDbDataReader
    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    ' Bind Data Reader to DropDownList
    ddlTeamName.DataSource = dr
    ddlTeamName.DataBind()

    Thanks.

    Regards,
    Hean

  7. #7
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    What is the error you are getting?
    Dont gain the world and lose your soul

  8. #8
    Lively Member
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    77
    You may need to specify the DataTextName and the DataTextfield but it should work fine with a datareader. This is how I do mine


    Code:
                    Dim f as new Listbox
                    f.EnableViewState = True
                    f.DataSource = dr
                    f.DataValueField = "code"
                    f.DataTextField = "name"
                    f.DataBind()
                    panel1.controls.add(f)
    "Find all you need in your mind if you take the time" -DT

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Posts
    11
    Yes ! it work if include with the setting of DataTextField & DataValueField.
    Thanks, Cin0s3 & thank you to everyone that gave the advice.

    Regards,
    Koay

  10. #10
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    DropDownList Bind

    Hi guys, I can't seem to get this DropDownList thing to work on my web form. Here is my code.

    VB Code:
    1. Dim cn As New SqlClient.SqlConnection()
    2.         Dim commSQL As New SqlClient.SqlCommand()
    3.  
    4.         cn.ConnectionString = "Integrated Security=True;" & _
    5.         "Data Source=Brutus;Initial Catalog=Problem Tracking;" & _
    6.         "user id=xxx;password=yyyyy;"
    7.         cn.Open()
    8.         commSQL.Connection = cn
    9.         commSQL.CommandText = "Select [Store Number] From Stores Order By [Store Number]"
    10.         Dim datRead As SqlClient.SqlDataReader
    11.         datRead = commSQL.ExecuteReader()
    12.         cmbStores.DataSource = datRead
    13.         cmbStores.DataBind()

    The field I am trying to place in the dropdown box is Stores.Store Number.

    I am getting the same errors the first person in this post got. I do not understand the fix that the other gave him.

    Can anyone help me please?

    Thanks
    David Wilhelm

  11. #11
    Lively Member
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    77
    Just do like this:
    Code:
    Dim cn As New SqlClient.SqlConnection()
            Dim commSQL As New SqlClient.SqlCommand()
    
            cn.ConnectionString = "Integrated Security=True;" & _
            "Data Source=Brutus;Initial Catalog=Problem Tracking;" & _
            "user id=xxx;password=yyyyy;"
            cn.Open()
            commSQL.Connection = cn
            commSQL.CommandText = "Select [Store Number] From Stores Order By [Store Number]"
            Dim datRead As SqlClient.SqlDataReader
            datRead = commSQL.ExecuteReader()
            cmbStores.DataSource = datRead
            cmbStores.datatextfield="Store Number"
            cmbStores.datavaluefield="Store Number"
            cmbStores.DataBind()
    "Find all you need in your mind if you take the time" -DT

  12. #12
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Still getting error

    Cin, I tried that and I still get the same error.
    David Wilhelm

  13. #13
    Lively Member
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    77
    Whats the error?
    "Find all you need in your mind if you take the time" -DT

  14. #14
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Error

    Cin, I don't really get an error. It's just that the dropdown box is filled with 32 lines of the text 'System.Data.Common.DbDataRecord' I need the actualy store number in this drop down box.

    David
    David Wilhelm

  15. #15
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Unhappy

    Nevermind, this works. I had the code in the wrong event.

    Lets say that I wanted to add the the text word Add to this dropdown box to be inserted before the stores. How would I do that?
    David Wilhelm

  16. #16
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Either add the items manually (looping through the datareader) and add that item before your loop

    or

    Save the result set to a datatable and use the INSERT on the datatable to add a row to the beginning of the datatable and then bind the datatable to the drop down list.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  17. #17
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I knew about the loop and add method.

    It is not a good idea for me to add 'All' to the database table.

    I was just thinking maybe there was a more efficient way to do this. The Loop method is fine since there are only 32 records in this particular table anyway.

    Thanks!
    David Wilhelm

  18. #18
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Just to make sure we are clear--

    I was not suggesting you add ALL to the database table, but rather a system.data.datatable object that you create on the fly.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  19. #19
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    After binding to the reader do this:
    VB Code:
    1. cmbStores.Items.Insert(0, New ListItem("Add", ""))

  20. #20
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Thanks pvb, that works great!

    Where do you guys learn all this stuff? Books? What books?

    Thanks
    David Wilhelm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width