Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a DropDownList 1

Status
Not open for further replies.

Phil5673

Programmer
Sep 30, 2002
42
US
I am attempting to populate a dropdownlist in a web page. When I run it, the list displays "System.Data.DataRowView" instead of my data. It displays this for each row I have in the table.

Here is the code I am using.

mports System.Data
Imports System.Data.Common
Imports Microsoft.Data.Odbc

Public Class WebForm1
Inherits System.Web.UI.Page

Dim myConnStr As String = "DSN=CedarHill_Disc;UID=edpdisc;PWD=mallard;"
Dim objConn As New OdbcConnection(myConnStr)
Dim objCmd As New OdbcCommand()
Dim objDA As New OdbcDataAdapter()
Dim dvw As DataView
Dim ds As New DataSet()
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
Dim strSQL As String

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then

strSQL = &quot;SELECT * FROM DI_C164_Disciplinary_Action_Codes&quot;
objDA = New OdbcDataAdapter(strSQL, objConn)
objDA.Fill(ds, &quot;D_A_C&quot;)
dvw = ds.Tables(&quot;D_A_C&quot;).DefaultView
DropDownList1.DataSource = dvw
DropDownList1.DataBind()
DropDownList1.DataValueField = &quot;Code&quot;
DropDownList1.DataTextField = &quot;Description&quot;
DataGrid1.DataSource = dvw
DataGrid1.DataBind()
End If
End Sub
End Class
 
The problem is that when you do the databind, the drop down list doesn't know what are the text and value fields, so it binds each item to the full DataView row.

Try inverting the order of your lines to:
DropDownList1.DataValueField = &quot;Code&quot;
DropDownList1.DataTextField = &quot;Description&quot;
DropDownList1.DataBind()
NetAngel
 
Thanks for your help. It always the &quot;obvious&quot; things we miss.

Phil5673
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top