Solved

Error binding Access table to dropdown box in ASP.Net

Posted on 2009-04-01
10
585 Views
Last Modified: 2012-05-06
I get this error even though there is a column "Employees_Name"
System.Web.HttpException: DataBinding: 'System.Data.Common.DataRecordInternal' does not contain a property with the name 'Employees_Name'.

Is there something wrong with my code?
// Code on Button
    <asp:DropDownList ID="ddFacilitator" AppendDataBoundItems = "true" runat="server">
    </asp:DropDownList>
 
//Code Behind
        Dim strQuerydd As String = "SELECT Employees_Name FROM Empname"
        Dim cmdSearchdd As New OleDbDataAdapter(strQuerydd, dbConn)
 
        Dim readerdd As OleDbDataReader
 
        Try
            'dbConn.Open()
            readerdd = cmdSearch.SelectCommand.ExecuteReader()
 
 
            ddFacilitator.DataSource = readerdd
            ddFacilitator.DataTextField = "EmpName"
            ddFacilitator.DataValueField = "Employees_Name"
            ddFacilitator.DataBind()
 
 
        Catch exdd As Exception
            Response.Write(exdd)
        End Try
        readerdd.Close()
        dbConn.Close()

Open in new window

0
Comment
Question by:ITHelper80
[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
  • 4
10 Comments
 
LVL 8

Expert Comment

by:JRockFL
ID: 24045879
This looks incorrect
ddFacilitator.DataTextField = "EmpName"

That is your table name.
Try
ddFacilitator.DataTextField = " Employees_Name"
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24045892
Hey JRock....Its been a while..what 3 hours? lol

I should just send you the questions directly.

I tired that and got a different error message
System.Web.HttpException: DataBinding: 'System.Data.Common.DataRecordInternal' does not contain a property with the name 'Employees_Name'. at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format) at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) at System.Web.UI.WebControls.ListControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at Facilitator.btnSearch_Click(Object sender, EventArgs e) in H:\Work Web Dev\Visions\Facilitator.aspx.vb:line 72 

Open in new window

0
 
LVL 8

Expert Comment

by:JRockFL
ID: 24045894
The other thing that catches my eye might be

readerdd = cmdSearch.SelectCommand.ExecuteReader()

Try
readerdd = cmdSearch.ExecuteReader()
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 6

Author Comment

by:ITHelper80
ID: 24045905
ASP.Net 3.5 wont let me do that.

It says ExecuteReader is not a member of OLEDBDataAdapter

The error goes away with the SelectCommand
0
 
LVL 8

Expert Comment

by:JRockFL
ID: 24045934
Sorry, I have not used OleDbDataAdapter in 3.5

Hmmmm
You are positive Employees_Name is the correct column name?

Maybe an alias would work

Dim strQuerydd As String = "SELECT Employees_Name As Name FROM Empname"
            ddFacilitator.DataTextField = "Name"
            ddFacilitator.DataValueField = "Name"
0
 
LVL 8

Expert Comment

by:JRockFL
ID: 24045941
'dbConn.Open()

The connection is closed?
dbConn.Open()
0
 
LVL 8

Expert Comment

by:JRockFL
ID: 24045955
Even though using a reader is a good choice, so can be a datatable.

    Private Sub LoadDropDowns()
        Dim dbConn As OleDbConnection
        Dim strQuerydd As String = "SELECT Employees_Name FROM Empname"
        Dim command As New OleDbCommand(strQuerydd, dbConn)
        Dim da As New OleDbDataAdapter(command)
        Dim dt As New DataTable()
 
        Try
 
            cmdSearchdd.Fill(dt)
 
            ddFacilitator.DataSource = dt
            ddFacilitator.DataTextField = "Employees_Name"
            ddFacilitator.DataValueField = "Employees_Name"
            ddFacilitator.DataBind()
 
 
        Catch exdd As Exception
            Response.Write(exdd)
        End Try
 
    End Sub

Open in new window

0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24045979
The ddConn.Open() is commented out. Here is my complete code behind

Also there is my DB...just in case i may be missing something
Imports System.Data.OleDb
Partial Class Facilitator
    Inherits System.Web.UI.Page
    Private dbConn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Visions_DB.mdb;Persist Security Info=False;")
 
 
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
 
 
        Dim strQuery As String = "SELECT Vis_Num,Status,Employee,Present_Method,Proposed_Method,Validator,Notes,ValDate FROM Data WHERE Vis_Num = ?"
        Dim cmdSearch As New OleDbDataAdapter(strQuery, dbConn)
 
        'cmdSearch.SelectCommand.Parameters.AddWithValue("Vis_Num", "%" & txtSearch.Text.ToString & "%")
        cmdSearch.SelectCommand.Parameters.AddWithValue("Vis_Num", CInt(txtSearch.Text))
        Dim reader As OleDbDataReader
 
        Try
            dbConn.Open()
            reader = cmdSearch.SelectCommand.ExecuteReader()
 
            If IsNothing(reader) = False Then
 
                txtError.Visible = True
                txtVisNum.Text = ""
                txtName.Text = ""
                txtName.Text = ""
                txtPresent.Text = ""
                txtProposed.Text = ""
                ddstatus.SelectedValue = ""
 
            End If
 
            While reader.Read()
 
                If Not reader("Vis_Num") Is DBNull.Value Then
                    txtVisNum.Text = reader("Vis_Num").ToString()
                    txtName.Text = reader("Employee").ToString()
                    txtPresent.Text = reader("Present_Method").ToString()
                    txtProposed.Text = reader("Proposed_Method").ToString()
                    'ddstatus.DataSource = reader
                    'ddstatus.DataValueField = "Status"
                    'ddstatus.DataTextField = "Data"
                    'ddstatus.Items.Insert(0, New ListItem("-- Status --"))
                    'ddstatus.DataBind()
 
                    txtError.Visible = False
                End If
 
            End While
 
        Catch ex As Exception
 
            Response.Write(ex)
 
        End Try
 
        reader.Close()
 
        Dim strQuerydd As String = "SELECT Employees_Name as Name FROM Empname"
        Dim cmdSearchdd As New OleDbDataAdapter(strQuerydd, dbConn)
 
        Dim readerdd As OleDbDataReader
 
        Try
            'dbConn.Open()
            readerdd = cmdSearch.SelectCommand.ExecuteReader()
 
 
            ddFacilitator.DataSource = readerdd
            ddFacilitator.DataTextField = "Name"
            ddFacilitator.DataValueField = "Name"
            ddFacilitator.DataBind()
 
 
        Catch exdd As Exception
            Response.Write(exdd)
        End Try
        readerdd.Close()
        dbConn.Close()
    End Sub
End Class

Open in new window

Visions-DB.mdb
0
 
LVL 8

Accepted Solution

by:
JRockFL earned 500 total points
ID: 24046039
This part looks incorrect
readerdd = cmdSearch.SelectCommand.ExecuteReader()
That is the commmand that is higher up in the code

i think you want the cmdSearchdd command
readerdd = cmdSearchdd.SelectCommand.ExecuteReader()

I hope that does it....my benadryl is starting to kick in.
if not i will be back in the am
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24046081
Sweet, once again you come to the rescue.

Thanks, Im sure we will speak tomorrow ;)
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

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