Hi,
I'm trying to bind a DropDownList in the code behind using the following code:

Dim DatabaseConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("myServer").ConnectionString)
Dim selectCMD As String = “SELECT name, id FROM Clients WHERE name LIKE @name + ‘%’”
Dim selectCMD As New SqlCommand(selectSQL, DatabaseConnection)
selectCMD.Parameters.Add(New SqlParameter("@name", SqlDbType.VarChar))
selectCMD.Parameters("@name").Value = name

Dim adapter As New SqlDataAdapter
Dim dTable As New DataTable
adapter.SelectCommand = selectCMD
adapter.Fill(dTable)

ddl.DataSource = dTable
ddl.DataBind()

But the values that are being bound to the DDL are all = System.Data.DataRowView. Any ideas of what is wrong?

Dani AI

Generated

The DropDownList shows "System.Data.DataRowView" because it is bound to DataRowView objects but the control wasn't told which column/property to use for display and value. As pointed out (and @AnaD confirmed), the control needs explicit DataTextField and DataValueField set before calling DataBind so it can render the column you expect instead of calling ToString() on each DataRowView.

Quick checklist for the same symptom:

  • Confirm the DataTable actually contains the expected columns and rows (inspect it in the debugger).
  • Make sure the names you use for the text/value fields exactly match the DataTable column names or object property names.
  • Set the text/value field properties before calling DataBind, and ensure nothing else rebinds the control afterward.
  • If your SQL uses LIKE, either include the wildcard in the parameter value or build the pattern on the SQL side consistently.
  • Watch for coding slips in the original snippet: mismatched variable names, smart/curly quotes from copy/paste, or accidental redeclaration of variables — those can hide the real bind data.

Good practices: wrap SqlConnection/SqlCommand in Using blocks so connections are always closed, set parameter sizes for varchar parameters, and inspect the bound items in the debugger (you should see DataRowView objects whose fields contain your columns). For Microsoft reference on the fields the list control uses, see the ListControl DataTextField/DataValueField documentation and the DataRowView type:
ListControl.DataTextField / DataValueField
DataRowView class

Recommended Answers

All 2 Replies

ddl.DataSource = dTable
ddl.DataTextField = "name"
ddl.DataValueField="id"
ddl.DataBind()

Hi Adatapost,

Thanks a lot! Now it works perfectly! =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.