Dropdownlist...

  • Thread starter Thread starter MGRideout
  • Start date Start date
M

MGRideout

Hello I have the following dropdown list...

Public Sub DisplayCity()

Dim objDBConn As New SqlConnection

Dim objDataView2 As New DataView
Dim objCity As New clsDBCity

objDataView2 = objCity.GetCities

ddlCity.DataSource = objDataView2
ddlCity.DataBind()

objDBConn.Close()

End Sub

Where objDataView2 is a SELECT statment from a SQL DB. So the DDL displays
the cities correctly and I can choose them no problem. The problem starts
when choose a city that is 3 or 4 down in the list and click submit on my
form (which writes to a DB table), the first city in the DDL is writtern to
the DB , NOT THE ONE I selected...
Please any ideas on this...
Thanks in advance!!

Mark
 
A DDL issue like that usually points to a duplicate
DataValueField



in the ddl.



example

DataValueField,DataTextField

NC , "North Carolina"
NC , "New Cananda"

The ddl displays "North Carolina" and "New Canada", but since both are
"NC"....it'll always find North Carolina.

Look at your html-source..and I'll bet you'll find a duplicate.


PS
Your code has a bad case of VB6 hangover.
Try to find some new habits and new coding standards.
 
Hey Sloan,

Nope there is no duplicates in my database table, which is where the data is
coming from... As for the VB6 hangover... what are your suggustions for "new"
coding standards??

Mark
 
In which event do you write to the DB ? Do you always call DisplayCity ? is
this an ASP.NET app ?

For now my guess is that you always rebind the data to the list clearing
the list state and writing then always the same entry. If this is the case
you'll have to use a "If Not IsPostback" construct to do that only on
initial load (google for this or check the doc for details).
 
Hey Patrice...Yes you were right, I was not using if Not Page.IsPostBack.

Thanks very much for your help...
on Another note: have you even seen a DDL (this is on an edit page), once my
data loads in the text boxes and DDLs, the DDL for City there appears two
cities (one in the list and the other is one from DB) in which I am
requesting to poluate the page...

Any ideas?
Thanks,

Mark
 
I'm glad he gave you the exact tip.

Remember....LOOK AT THE HTML SOURCE,,not just the underlying database table.

You would have seen this double-upping in the html-output...and then could
have tried to gotten to why it was double-populating.
(Which was the if(!Page.IsPostBack) check)

...
 
Back
Top