473,473 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dropdown list won't display selected item

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).ToStr ing
ddList.DataValueField =
ds.Tables(sLookupTable).Columns(sKeyColumn).ToStri ng
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.
Oct 24 '06 #1
1 1664
Ok - it's because I hadn't specified ddList.DataBind() and just used
DataBind() instead. Doh!

Justin.

"Justin" wrote:
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).ToStr ing
ddList.DataValueField =
ds.Tables(sLookupTable).Columns(sKeyColumn).ToStri ng
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.

Oct 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kris Rockwell | last post by:
Hello (again), I have gotten the dropdown list functionality to work through a few tricks (probably not the most efficient, but it works) but I am not sure how to set the default selected value....
6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
6
by: Jenna Alten | last post by:
I have a datagrid with a template column that contains a dropdown list. I currently fill and display the dropdown list on the page load. This is working correctly. I am NOT using an Edit Column. I...
3
by: RJN | last post by:
Hi The texts in the dropdown are too long and the width is not sufficient to show the entire text. Increasing the width is not an option. Is there a way to show the selected item text as a...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
6
by: nishac | last post by:
Can anyone suggest me how to make my drop down menu work in IE7 too.Its working in other browsers.On mouse over the submenus should be displayed.Am attaching my css code hereby.Anyone please check...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
5
by: =?Utf-8?B?Y2hlY2tyYWlzZXJAY29tbXVuaXR5Lm5vc3BhbQ== | last post by:
I have a VS 2008 ASP.NET webform that has a reportview tag on it, accessing an .RLDC report in local report. The columns for the report are essentially: Month Item #1 Item#2 Item#3 ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.