Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to set a default selection of a ddl querying the database

Status
Not open for further replies.

cesark

Programmer
Dec 20, 2003
621
I am calling two Subroutines from the main Sub in order to maintain four dropdownlists form object selections querying the database when the page is load. The four ddl are: States_1, Cities_1, States_2, Cities_2. So, two identical pair of dependant ddl States-Cities.

The only problem I have is with the second pair of ddl, which Cities_2 ddl is correctly populated with the correspondent cities for the specified State_2, but I can’ t set the default selection returned by the DB, which is returned correctly as a number.

Here is the code, the line in bold at the bottom is what it doesn’ t work:
Code:
‘ Inside the main Sub

...

        State_1.SelectedValue = CmdRetrieves.Parameters("@State_1_num").Value	
	    Dim State_Em As Int16 = CmdRetrieves.Parameters("@State_1_num").Value
	    Dim cities_Em As Int32 = CmdRetrieves.Parameters("@city_1_num").Value
	    Call city_Session_Em(State_Em, cities_Em)



        State_2.SelectedValue = CmdRetrieves.Parameters("@State_2_num").Value
	    Dim State_Or As Int16 = CmdRetrieves.Parameters("@State_2_num").Value
	    If Not CmdRetrieves.Parameters("@city_2_num").Value Is DbNull.Value Then   
		 Dim city_Or As Int32 = CmdRetrieves.Parameters("@city_2_num").Value
		 Call city_Session_Or(State_Or, city_Or)
		Else
		 Dim city_Or As Int32 = "0"		
		 Call city_Session_Or(State_Or, city_Or)
		End If

...




Sub city_Session_Em(ByVal State_Em As Int16, ByVal cities_Em As Int32)

  Dim CmdCitiesState As New sqlCommand ("Cities_by_State", strConnection)
  CmdCitiesState.CommandType = CommandType.StoredProcedure
  
  CmdCitiesState.Parameters.Add(New SqlParameter("@State", SqlDbType.SmallInt, 2))
  CmdCitiesState.Parameters("@State").Value = State_Em
  
  Dim citiesSel As SqlDataReader = CmdCitiesState.ExecuteReader

  cities_Emp.DataSource = citiesSel
  cities_Emp.DataBind()
  
  citiesSel.Close()
  
   Dim listC_Em As New ListItem
   listC_Em.Text = "(Select)"
   listC_Em.Value = "0"
   cities_Emp.Items.Insert(0, listC_Em)
   cities_Emp.SelectedValue = cities_Em

End Sub




Sub city_Session_Or(ByVal State_Or As Int16, ByVal city_Or As Int32)

  Dim CmdCitiesState As New sqlCommand ("Cities_by_State", strConnection)
  CmdCitiesState.CommandType = CommandType.StoredProcedure
  
  CmdCitiesState.Parameters.Add(New SqlParameter("@State", SqlDbType.SmallInt, 2))
  CmdCitiesState.Parameters("@State").Value = State_Or
  
  Dim citiesSel As SqlDataReader = CmdCitiesState.ExecuteReader(CommandBehavior.CloseConnection)

  cities_ori.DataSource = citiesSel
  cities_ori.DataBind()
  
  
   Dim listC_Or As New ListItem
   listC_Or.Text = "(Select)"
   listC_Or.Value = "0"
   cities_ori.Items.Insert(0, listC_Or)
   [b]cities_ori.SelectedValue = city_Or[/b]

End Sub

Thank you,
Cesar
 
Hi,

Try the selectedIndex property, not the selectedValue

For Each item As ListItem In cboLenderId.Items
If CInt(item.Value) = ucWizard.CustomerID Then
cboLenderId.SelectedIndex = cboLenderId.Items.IndexOf(item)
End If
Next

HTH

amit
 
I already solved the problem.

Some lines down of:
Call city_Session_Or(State_Or, city_Or)

I called a command that populated the ddl cities_ori again, this time with SelectedIndex set to '0'.

I am sorry, it was my fault.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top