Ive successfully managed to bind my DropDownList control to the membership.getAllUsers class. The problem is, no matter who i select from the list, it always returns the same value which is the first selection. Is this some kind of postback issue? Yikes!

Protected Sub fillDropList(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DropList1.DataSource = Membership.GetAllUsers()
        DropList1.DataBind()
    End Sub
    
Protected Sub DropList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropList1.SelectedIndexChanged
        results.Text = DropList1.SelectedItem.ToString()

    End Sub

Recommended Answers

Your dropdownlist bound to datasource for every submit. It should not be.

You need to check IsPostBack property on load event before binding the DropDownList.

Change your code as below

Protected Sub fillDropList(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
If Not IsPostBack Then
   DropList1.DataSource = …
Jump to Post

Have you specified

DropDownList1.DataTextField = "";
        DropDownList1.DataValueField="";
Jump to Post

All 5 Replies

Your dropdownlist bound to datasource for every submit. It should not be.

You need to check IsPostBack property on load event before binding the DropDownList.

Change your code as below

Protected Sub fillDropList(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
If Not IsPostBack Then
   DropList1.DataSource = Membership.GetAllUsers()        
   DropList1.DataBind()    
End If
End Sub

Have you specified

DropDownList1.DataTextField = "";
        DropDownList1.DataValueField="";

Solved!!!
Thank You. All is well now.

Bind the Dropdown when not ispostback . Otherwise Dropdown rebinds on every postback

Ive tested the values for both gridView1 and dropList1 and they are exactly what I want them to be. Too bad I cant figure why the above error keeps popping up. Im no .net expert so im hoping this is just some kind of syntax or inconsistent dataType. Please help

Imports System.Security
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Data


Partial Class admin
    Inherits System.Web.UI.Page
    Public connectPath As String = ConfigurationSettings.AppSettings("Maya")


    Protected Sub fillDropList(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            DropList1.DataSource = Membership.GetAllUsers()
            DropList1.DataBind()
        End If
    End Sub

    Protected Sub DropList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropList1.SelectedIndexChanged
        Dim theUser2 As Integer = gridView1.SelectedDataKey.Value
        Dim con As New SqlConnection
        con.ConnectionString = connectPath

        'the customerID of the selected gridview row


        Dim setRep As New SqlCommand
        setRep.Connection = con
        setRep.CommandText = "INSERT INTO dbo.customers(rep)VALUES(@dropList1)WHERE rep='" & theUser2.ToString() & "'"
        setRep.Parameters.AddWithValue("@dropList1", DropList1.SelectedItem.ToString())

        Dim added As Integer = 0
        Try
            con.Open()

            added = setRep.ExecuteNonQuery()

            results.Text = added.ToString() & "record has been added!"



        Catch Err As Exception
            results.Text = "Error inserting record. "
            results.Text &= Err.Message
        Finally
            con.Close()
        End Try


    End Sub


End Class
Be a part of the DaniWeb community

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