943,985 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 925
  • ASP.NET RSS
Oct 27th, 2009
0

DropDownList only returns the 1st value regardless of selection

Expand Post »
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!

ASP.NET Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 9
Solved Threads: 0
Light Poster
ricksvoid is offline Offline
39 posts
since May 2008
Oct 28th, 2009
1
Re: DropDownList only returns the 1st value regardless of selection
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
ASP.NET Syntax (Toggle Plain Text)
  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; Oct 28th, 2009 at 2:13 am.
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009
Oct 28th, 2009
0
Re: DropDownList only returns the 1st value regardless of selection
Have you specified
ASP.NET Syntax (Toggle Plain Text)
  1. DropDownList1.DataTextField = "";
  2. DropDownList1.DataValueField="";
Reputation Points: 10
Solved Threads: 12
Posting Whiz in Training
carobee is offline Offline
209 posts
since Dec 2007
Oct 28th, 2009
0

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

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

ASP.NET Syntax (Toggle Plain Text)
  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.  
Reputation Points: 9
Solved Threads: 0
Light Poster
ricksvoid is offline Offline
39 posts
since May 2008
Oct 28th, 2009
0
Re: DropDownList only returns the 1st value regardless of selection
Solved!!!
Thank You. All is well now.
Reputation Points: 9
Solved Threads: 0
Light Poster
ricksvoid is offline Offline
39 posts
since May 2008
Oct 28th, 2009
0
Re: DropDownList only returns the 1st value regardless of selection
Bind the Dropdown when not ispostback . Otherwise Dropdown rebinds on every postback
Reputation Points: 10
Solved Threads: 1
Newbie Poster
parmesh.a is offline Offline
1 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Opera most used version (besides latest)
Next Thread in ASP.NET Forum Timeline: how to get the header name for the datatable in C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC