Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,065
    Thanks
    4
    Thanked 8 Times in 8 Posts

    dropdownlist in asp.net

    My drop down list works but no matter what the user selects as an option it always returns the ist line after the form posts. Anyone know what the problem is?

    here is the code
    Code:
    Sub Page_Load(sender as Object, e as EventArgs) 
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; "& _ 
            "Data Source=C:\Documents and Settings\Miranda\My Documents\Dobyns\mydb.mdb" 
        Dim dbConnection As New OleDbConnection(connectionString) 
        dbConnection.Open() 
        Dim queryString As String = "SELECT stateCode, state FROM usaStates ORDER by state" 
        Dim dbCommand As New OleDbCommand(queryString,dbConnection) 
        Dim objDR as OleDbDataReader = dbCommand.ExecuteReader() 
        'Databind the DataReader to the listbox Web control 
        lstStates.DataSource = objDR 
        lstStates.DataBind() 
    
        'Add a new listitem to the beginning of the listitem collection 
        lstStates.Items.Insert(0, new ListItem("-- Choose a State --")) 
    
        dbConnection.Close() 
    
    End Sub 
    
    Sub Button1_Click(sender As Object, e As EventArgs)
            If Page.IsValid Then
                Dim customerDS As New DataSet
    
                customerDS = CheckForRecord(company.Text,address1.Text,City.Text,lstStates.SelectedItem.Value)
    
                If customerDS.Tables(0).Rows.Count = 1 Then
                    msgLabel.Text = "Customer Exists"
                ElseIf NewCustomer(company.Text, address1.Text, address2.Text, city.Text, lstStates.SelectedItem.Value, zip.Text, phone.Text, TollFree.Text, fax.Text, contactName.Text, extension.Text, email.text, isShipper.Checked, isConsignee.Checked, isCarrier.Checked, isThirdParty.Checked, comments.Text) > 0 Then
                    msgLabel.Text = "Success"
                    DataGrid1.DataSource = GetCustomers()
                    DataGrid1.DataBind()
                    'ResetAllFields()
                End If
    
            End If
    End Sub
    
    'add new customer
        Function NewCustomer( _
                    ByVal company As String,  _
                    ByVal address1 As String,  _
                    ByVal address2 As String,  _
                    ByVal city As String,  _
                    ByVal state As String,  _
                    ByVal zip As String,  _
                    ByVal contactName As String,  _
                    ByVal phone As String,  _
                    ByVal tollfree As String,  _
                    ByVal extension As String,  _
                    ByVal fax As String,  _
                    ByVal email As String, _
                    ByVal isShipper As Boolean,  _
                    ByVal isConsignee As Boolean,  _
                    ByVal isThirdParty As Boolean,  _
                    ByVal isCarrier As Boolean,  _
                    ByVal comments As String) As Integer
            Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; "& _
                "Data Source=C:\Documents and Settings\Miranda\My Documents\Dobyns\mcdb.mdb"
            Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
    
            Dim queryString As String = "INSERT INTO customers (company,address1,address2,city,state,zip,contactName,"& _
                "phone,tollfree,extension,fax,email,isShipper,isConsignee,isThirdParty,isCarrier,comments) "& _
                "VALUES (@company, @address1, @address2, @city, @state, @zip, @contactName, @phone, "& _
                "@tollfree, @extension, @fax, @email, @isShipper, @isConsignee, @isThirdParty, @isCarrier, @comments)"
            Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
            dbCommand.CommandText = queryString
            dbCommand.Connection = dbConnection
    
            Dim dbParam_company As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_company.ParameterName = "@company"
            dbParam_company.Value = company
            dbParam_company.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_company)
            Dim dbParam_address1 As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_address1.ParameterName = "@address1"
            dbParam_address1.Value = address1
            dbParam_address1.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_address1)
            Dim dbParam_address2 As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_address2.ParameterName = "@address2"
            dbParam_address2.Value = address2
            dbParam_address2.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_address2)
            Dim dbParam_city As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_city.ParameterName = "@city"
            dbParam_city.Value = city
            dbParam_city.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_city)
            Dim dbParam_state As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_state.ParameterName = "@state"
            dbParam_state.Value = state
            dbParam_state.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_state)
            Dim dbParam_zip As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_zip.ParameterName = "@zip"
            dbParam_zip.Value = zip
            dbParam_zip.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_zip)
            Dim dbParam_contactName As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_contactName.ParameterName = "@contactName"
            dbParam_contactName.Value = contactName
            dbParam_contactName.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_contactName)
            Dim dbParam_phone As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_phone.ParameterName = "@phone"
            dbParam_phone.Value = phone
            dbParam_phone.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_phone)
            Dim dbParam_tollfree As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_tollfree.ParameterName = "@tollfree"
            dbParam_tollfree.Value = tollfree
            dbParam_tollfree.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_tollfree)
            Dim dbParam_extension As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_extension.ParameterName = "@extension"
            dbParam_extension.Value = extension
            dbParam_extension.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_extension)
            Dim dbParam_fax As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_fax.ParameterName = "@fax"
            dbParam_fax.Value = fax
            dbParam_fax.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_fax)
            Dim dbParam_email As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_email.ParameterName = "@email"
            dbParam_email.Value = email
            dbParam_email.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_email)
            Dim dbParam_isShipper As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_isShipper.ParameterName = "@isShipper"
            dbParam_isShipper.Value = isShipper
            dbParam_isShipper.DbType = System.Data.DbType.Boolean
            dbCommand.Parameters.Add(dbParam_isShipper)
            Dim dbParam_isConsignee As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_isConsignee.ParameterName = "@isConsignee"
            dbParam_isConsignee.Value = isConsignee
            dbParam_isConsignee.DbType = System.Data.DbType.Boolean
            dbCommand.Parameters.Add(dbParam_isConsignee)
            Dim dbParam_isThirdParty As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_isThirdParty.ParameterName = "@isThirdParty"
            dbParam_isThirdParty.Value = isThirdParty
            dbParam_isThirdParty.DbType = System.Data.DbType.Boolean
            dbCommand.Parameters.Add(dbParam_isThirdParty)
            Dim dbParam_isCarrier As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_isCarrier.ParameterName = "@isCarrier"
            dbParam_isCarrier.Value = isCarrier
            dbParam_isCarrier.DbType = System.Data.DbType.Boolean
            dbCommand.Parameters.Add(dbParam_isCarrier)
            Dim dbParam_comments As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_comments.ParameterName = "@comments"
            dbParam_comments.Value = comments
            dbParam_comments.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_comments)
    
            Dim rowsAffected As Integer = 0
            dbConnection.Open
            Try
                rowsAffected = dbCommand.ExecuteNonQuery
            Catch
                msgLabel.Text = lstStates.SelectedItem.Value
                'msgLabel.Text = "Database Error"
                msgLabel.Visible = true
            Finally
                dbConnection.Close
            End Try
    
            Return rowsAffected
        End Function
    and here is the tag itself
    Code:
    <asp:DropDownList id="lstStates" runat="server" DataValueField="stateCode" DataTextField="state" Rows="1"></asp:DropDownList>
    can anyone tell me why it always returns -- Choose a State -- ??

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    with the postback you rebuild the entire dropdownlist. You should check in your Page_Load handler for the Page.IsPostBack member, to see if it you should rebuild the dropdown. If not, handle the click event

    So your Page_load should look like this:
    Code:
    Sub Page_Load(sender as Object, e as EventArgs) 
      IF Not Page.IsPostBack Then
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; "& _ 
            "Data Source=C:\Documents and Settings\Miranda\My Documents\Dobyns\mydb.mdb" 
        Dim dbConnection As New OleDbConnection(connectionString) 
        dbConnection.Open() 
        Dim queryString As String = "SELECT stateCode, state FROM usaStates ORDER by state" 
        Dim dbCommand As New OleDbCommand(queryString,dbConnection) 
        Dim objDR as OleDbDataReader = dbCommand.ExecuteReader() 
        'Databind the DataReader to the listbox Web control 
        lstStates.DataSource = objDR 
        lstStates.DataBind() 
    
        'Add a new listitem to the beginning of the listitem collection 
        lstStates.Items.Insert(0, new ListItem("-- Choose a State --")) 
    
        dbConnection.Close() 
      Else
        'do other initialisation stuff
      End If
    End Sub
    I am the luckiest man in the world

  • #3
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,065
    Thanks
    4
    Thanked 8 Times in 8 Posts

    Smile Thanks Roelf

    I found out from another board that was what I was missing. All of the search results I found on that and not one of them mentioned to check for Page.IsPostBack.


  •  

    Posting Permissions

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