G
Guest
Hi all,
I have a bunch of drop-down lists that should each display an item (from a
SQL Server lookup table) based on another piece of data from a separate table.
For example;
If Not Page.IsPostBack Then
..
..
sSelVal = ds.Tables("propDetails").Rows(0)("work_type_id")
rc = BindListToDataSet(propDetails_lstWorkType, "work_type",
"work_type_id", "descr", sSelVal)
..
..
End If
..
..
Private Function BindListToDataSet(ByRef ddList As DropDownList, ByVal
sLookupTable As String, ByVal sKeyColumn As String, ByVal sDataColumn As
String, Optional ByVal sSelectedValue As String = "") As Long
Dim objConn As New System.Data.OleDb.OleDbConnection
Dim sSQL As String
Dim rc As Long
objConn.ConnectionString = "<my conn string>"
Dim ds As New DataSet(sLookupTable)
sSQL = "select " & sKeyColumn & ", " & sDataColumn & " from " &
sLookupTable
Dim objCMD As New OleDb.OleDbDataAdapter(sSQL, objConn)
objCMD.Fill(ds, sLookupTable)
If ds.Tables(sLookupTable).Rows.Count < 1 Then
'No matching rows
rc = -1
Else
rc = 0
ddList.DataTextField =
ds.Tables(sLookupTable).Columns(sDataColumn).ToString
ddList.DataValueField =
ds.Tables(sLookupTable).Columns(sKeyColumn).ToString
ddList.DataSource = ds.Tables(sLookupTable).DefaultView
DataBind()
If Len(Trim(sSelectedValue)) > 0 Then
Try
ddList.ClearSelection()
ddList.SelectedValue = sSelectedValue
Catch ex As Exception
ddList.SelectedIndex = -1
End Try
rc = ddList.SelectedIndex
End If
End If
Return rc
End Function
Ok, so this function populates the list correctly and, as far as I've been
able to trace what is happening, it does identify the value it is supposed to
be selecting and also returns the correct SelectedIndex value in rc, but the
form always displays the first item in the list (as though SelectedIndex is
zero).
I've probably done something stupid but just can't see what it is yet. If
anyone could shed some light on this for me I'd be very grateful indeed.
Thanks in advance,
Justin.
I have a bunch of drop-down lists that should each display an item (from a
SQL Server lookup table) based on another piece of data from a separate table.
For example;
If Not Page.IsPostBack Then
..
..
sSelVal = ds.Tables("propDetails").Rows(0)("work_type_id")
rc = BindListToDataSet(propDetails_lstWorkType, "work_type",
"work_type_id", "descr", sSelVal)
..
..
End If
..
..
Private Function BindListToDataSet(ByRef ddList As DropDownList, ByVal
sLookupTable As String, ByVal sKeyColumn As String, ByVal sDataColumn As
String, Optional ByVal sSelectedValue As String = "") As Long
Dim objConn As New System.Data.OleDb.OleDbConnection
Dim sSQL As String
Dim rc As Long
objConn.ConnectionString = "<my conn string>"
Dim ds As New DataSet(sLookupTable)
sSQL = "select " & sKeyColumn & ", " & sDataColumn & " from " &
sLookupTable
Dim objCMD As New OleDb.OleDbDataAdapter(sSQL, objConn)
objCMD.Fill(ds, sLookupTable)
If ds.Tables(sLookupTable).Rows.Count < 1 Then
'No matching rows
rc = -1
Else
rc = 0
ddList.DataTextField =
ds.Tables(sLookupTable).Columns(sDataColumn).ToString
ddList.DataValueField =
ds.Tables(sLookupTable).Columns(sKeyColumn).ToString
ddList.DataSource = ds.Tables(sLookupTable).DefaultView
DataBind()
If Len(Trim(sSelectedValue)) > 0 Then
Try
ddList.ClearSelection()
ddList.SelectedValue = sSelectedValue
Catch ex As Exception
ddList.SelectedIndex = -1
End Try
rc = ddList.SelectedIndex
End If
End If
Return rc
End Function
Ok, so this function populates the list correctly and, as far as I've been
able to trace what is happening, it does identify the value it is supposed to
be selecting and also returns the correct SelectedIndex value in rc, but the
form always displays the first item in the list (as though SelectedIndex is
zero).
I've probably done something stupid but just can't see what it is yet. If
anyone could shed some light on this for me I'd be very grateful indeed.
Thanks in advance,
Justin.