0

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
4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by ricksvoid
1

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

Edited by Ramesh S: n/a

0

Have you specified

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

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

0

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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.