Reputation: 570
In my web form, my drop down list already have the code to load the data selection. Now I want to add an auto fill function based on the query string in URL.
Private Sub FillLine() '1st dropdown
'1. Load intial selection into drop down list (OK)
'----------------------------------------------------------------------------------------------------
Dim dt As New DataTable
Dim strSql = "select distinct level1_id, [LCODE1]+ ' | '+[LNAME1] as [LCODE1]" _
& " FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP]"
Using conn As New SqlConnection(ConStr),
cmd As New SqlCommand(strSql, conn)
conn.Open()
dt.Load(cmd.ExecuteReader)
End Using
If dt.Rows.Count > 0 Then
line.DataSource = dt
line.DataTextField = "LCODE1"
line.DataValueField = "level1_id"
line.DataBind()
line.Items.Insert(0, "")
End If
'----------------------------------------------------------------------------------------------------
'2. Pick out the value in drop down list based on URL query string (NG)
'----------------------------------------------------------------------------------------------------
Dim LID As String = Request.QueryString("LID")
If LID <> Nothing Then
LID3.Text = LID
If Not IsPostBack() Then
Dim cmdLID As New SqlCommand("select distinct [LCODE1]+ ' | '+[LNAME1] as [LCODE1] " _
& "FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] " _
& "where [LEVEL3_ID] = '" & LID3.Text & "'", conn)
conn.Open()
Dim rdr As SqlDataReader = cmdLID.ExecuteReader
While rdr.Read
line.Text = rdr("LCODE1")
End While
conn.Close()
End If
End If
'----------------------------------------------------------------------------------------------------
End Sub
Protected Sub line_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim level1_ID As Integer = Convert.ToInt32(line.SelectedValue.ToString())
Dim value As Integer = Integer.Parse(level1_ID)
FillProcess(level1_ID)
Catch ex As Exception
Console.WriteLine(ex)
End Try
'FillProcess(level1_ID)
End Sub
During testing, part 1 of my code is fine. However, in part 2, every time I input the LID
query string in my URL and refresh the page, the value won't show in my drop down list.
What may be the issue here?
Upvotes: 0
Views: 885
Reputation: 15091
I am using one of my databases to test. Part 1 is the same as your code.
Part 2
It is not necessary to hit the database again. The values you need are in the DropDownList. I use a hard coded value for LID for testing but just substitute your query string value. Loop through the items in the list until you find the value you are looking for.
Private Sub FillDropDownList()
'1. Load intial selection into drop down list (OK)
Dim dt As New DataTable
Using cn As New SqlConnection(My.Settings.CoffeeConnection),
cmd As New SqlCommand("Select Top 10 ID, Name From Coffees", cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
DropDownList1.DataTextField = "Name"
DropDownList1.DataValueField = "ID"
DropDownList1.DataSource = dt
DropDownList1.DataBind()
'2. Pick out the value in drop down list based on URL query string
Dim LID As String = "14" 'Request.QueryString("LID")
For Each item As ListItem In DropDownList1.Items
If item.Value = LID Then
item.Selected = True
Exit For
End If
Next
End Sub
Upvotes: 0