Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: DropDownList not populating


Message #1 by "Mich Lang" <mlang11@h...> on Fri, 3 Jan 2003 14:31:48
Hi, I am new to asp.net. I have a dropdownlist that is not populating. I 
used the Server Explorer to connect to my SQL Server 2000 database. This 
created the SQLConnector and the SQLAdapter (I am connecting to a view). I 
created the Dataset using the wizard. I have used the Data menu choice and 
then the Preview data and the correct data is displayed. So I know the 
connection is working. I have written the following code to connect the 
dataset to the dropdownlist but it is not working. I am wondering if the 
code execution timing is off (meaning I am connecting the Dataset to the 
dropdownlist before the dataset has any data in it. Is this a 
possibility?). Or am I missing a step to load a drop down list? Any help 
is appreciated.

***********************
webform1.aspx.vb
***********************
Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents lblDetachment1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblDetachment2 As System.Web.UI.WebControls.Label
    Public WithEvents SQLCnnBadgeID As System.Data.SqlClient.SqlConnection
    Public WithEvents dbBadgeID As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents dsBadgeID2 As OPPApplication.dsBadgeID
    Protected WithEvents SqlSelectCommand1 As 
System.Data.SqlClient.SqlCommand
Protected WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents ddlDetachment1 As 
System.Web.UI.WebControls.DropDownList


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub 
InitializeComponent()
        Me.SQLCnnBadgeID = New System.Data.SqlClient.SqlConnection()
        Me.dbBadgeID = New System.Data.SqlClient.SqlDataAdapter()
        Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand()
        Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand()
        Me.dsBadgeID2 = New OPPApplication.dsBadgeID()
        CType(Me.dsBadgeID2, 
System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'SQLCnnBadgeID
        '
        Me.SQLCnnBadgeID.ConnectionString = "data source=DEVSERVER;initial 
catalog=Dev;password=prev;persist security info=Tr" & _
        "ue;user id=sa;workstation id=JUS00SPC458751;packet size=4096"
        '
        'dbBadgeID
        '
        Me.dbBadgeID.InsertCommand = Me.SqlInsertCommand1
        Me.dbBadgeID.SelectCommand = Me.SqlSelectCommand1
        Me.dbBadgeID.TableMappings.AddRange(New 
System.Data.Common.DataTableMapping() {New 
System.Data.Common.DataTableMapping("Table", "uvwBadgeEmpNo", New 
System.Data.Common.DataColumnMapping() {New 
System.Data.Common.DataColumnMapping("BadgeEmpNo", "BadgeEmpNo")})})
        '
        'SqlInsertCommand1
        '
        Me.SqlInsertCommand1.CommandText = "INSERT INTO uvwBadgeEmpNo
(BadgeEmpNo) VALUES (@BadgeEmpNo); SELECT BadgeEmpNo FRO" & _
        "M uvwBadgeEmpNo"
        Me.SqlInsertCommand1.Connection = Me.SQLCnnBadgeID
        Me.SqlInsertCommand1.Parameters.Add(New 
System.Data.SqlClient.SqlParameter("@BadgeEmpNo", 
System.Data.SqlDbType.VarChar, 9, "BadgeEmpNo"))
        '
        'SqlSelectCommand1
        '
        Me.SqlSelectCommand1.CommandText = "SELECT BadgeEmpNo FROM 
uvwBadgeEmpNo"
        Me.SqlSelectCommand1.Connection = Me.SQLCnnBadgeID
        '
        'dsBadgeID2
        '
        Me.dsBadgeID2.DataSetName = "dsBadgeID"
        Me.dsBadgeID2.Locale = New System.Globalization.CultureInfo("en-
US")
        Me.dsBadgeID2.Namespace = "http://www.tempuri.org/dsBadgeID.xsd"
        CType(Me.dsBadgeID2, 
System.ComponentModel.ISupportInitialize).EndInit()

    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
        ' fill dataset with data from user view 
        dbBadgeID.Fill(dsBadgeID2)

        'bind drop down list to data 
        ddlDetachment1.DataSource = dsBadgeID2
        ddlDetachment1.DataMember = "uvwBadgeEmpNo"
        ddlDetachment1.DataTextField = "BadgeEmpNo"
        ddlDetachment1.DataBind()

  Return to Index