DropDownList only returns the 1st value regardless of selection

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 34
Reputation: ricksvoid is an unknown quantity at this point 
Solved Threads: 0
ricksvoid ricksvoid is offline Offline
Light Poster

DropDownList only returns the 1st value regardless of selection

 
0
  #1
31 Days Ago
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!

  1.  
  2. Protected Sub fillDropList(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  3. DropList1.DataSource = Membership.GetAllUsers()
  4. DropList1.DataBind()
  5. End Sub
  6.  
  7. Protected Sub DropList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropList1.SelectedIndexChanged
  8. results.Text = DropList1.SelectedItem.ToString()
  9.  
  10. End Sub
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 433
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training
 
1
  #2
31 Days Ago
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
  1. Protected Sub fillDropList(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2. If Not IsPostBack Then
  3. DropList1.DataSource = Membership.GetAllUsers()
  4. DropList1.DataBind()
  5. End If
  6. End Sub
Last edited by Ramesh S; 31 Days Ago at 2:13 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 205
Reputation: carobee is an unknown quantity at this point 
Solved Threads: 11
carobee carobee is offline Offline
Posting Whiz in Training
 
0
  #3
31 Days Ago
Have you specified
  1. DropDownList1.DataTextField = "";
  2. DropDownList1.DataValueField="";
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 34
Reputation: ricksvoid is an unknown quantity at this point 
Solved Threads: 0
ricksvoid ricksvoid is offline Offline
Light Poster

Error inserting record. Incorrect syntax near the keyword 'WHERE'.

 
0
  #4
31 Days Ago
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

  1. Imports System.Security
  2. Imports System.Data.SqlClient
  3. Imports System.Web.Configuration
  4. Imports System.Data
  5.  
  6.  
  7. Partial Class admin
  8. Inherits System.Web.UI.Page
  9. Public connectPath As String = ConfigurationSettings.AppSettings("Maya")
  10.  
  11.  
  12. Protected Sub fillDropList(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  13. If Not Page.IsPostBack Then
  14. DropList1.DataSource = Membership.GetAllUsers()
  15. DropList1.DataBind()
  16. End If
  17. End Sub
  18.  
  19. Protected Sub DropList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropList1.SelectedIndexChanged
  20. Dim theUser2 As Integer = gridView1.SelectedDataKey.Value
  21. Dim con As New SqlConnection
  22. con.ConnectionString = connectPath
  23.  
  24. 'the customerID of the selected gridview row
  25.  
  26.  
  27. Dim setRep As New SqlCommand
  28. setRep.Connection = con
  29. setRep.CommandText = "INSERT INTO dbo.customers(rep)VALUES(@dropList1)WHERE rep='" & theUser2.ToString() & "'"
  30. setRep.Parameters.AddWithValue("@dropList1", DropList1.SelectedItem.ToString())
  31.  
  32. Dim added As Integer = 0
  33. Try
  34. con.Open()
  35.  
  36. added = setRep.ExecuteNonQuery()
  37.  
  38. results.Text = added.ToString() & "record has been added!"
  39.  
  40.  
  41.  
  42. Catch Err As Exception
  43. results.Text = "Error inserting record. "
  44. results.Text &= Err.Message
  45. Finally
  46. con.Close()
  47. End Try
  48.  
  49.  
  50. End Sub
  51.  
  52.  
  53. End Class
  54.  
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 34
Reputation: ricksvoid is an unknown quantity at this point 
Solved Threads: 0
ricksvoid ricksvoid is offline Offline
Light Poster
 
0
  #5
31 Days Ago
Solved!!!
Thank You. All is well now.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: parmesh.a is an unknown quantity at this point 
Solved Threads: 1
parmesh.a parmesh.a is offline Offline
Newbie Poster
 
0
  #6
31 Days Ago
Bind the Dropdown when not ispostback . Otherwise Dropdown rebinds on every postback
Parmesh
videozcorner
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC