 |
BOOK: Beginning ASP.NET 1.0  | This is the forum to discuss the Wrox book Beginning ASP.NET 1.0 with C# by Chris Goode, John Kauffman, Christopher L. Miller, Neil Raybould, S. Srinivasa Sivakumar, Dave Sussman, Ollie Cornes, Rob Birdwell, Matt Butler, Gary Johnson, Ajoy Krishnamoorthy, Juan T. Llibre, Chris Ullman; ISBN: 9780764543708 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 1.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

December 30th, 2004, 10:10 PM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Drop Down List
Hello,
I have a ddl that displays cities for the user to select.
ID City
1 Toronto
2 St. John's
3 Halifax
If the user selects St. John's (ID =2), the ID 2 gets wrote to the database for that user..this is fine, however when the user comes back to the page and changes some other data and NOT the city, St. John's gets ID=1 in the ddl, so when the user writes to the database again, the city changes to Toronto (ID=1)...
HOW DO I GET AROUND THIS. I am programming in VB.NET and using a Access database.
Please any help is greatly appricated.
Mark
|
|

December 31st, 2004, 06:04 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
When you load the page for editing, you should preselect the required item in the drop down.
There are various ways to do this. You can set the SelectedIndex of the drop down but a more suitable solution is probably to find the item by its value, and then select it, like this:
If Not lstMyList.Items.FindByValue("1") Is Nothing Then
lstMyList.Items.FindByValue("1").Selected = True
End If
This code tries to find an item in the dropdown list by its value, "1" in this case. If it is found, it is selected by setting the Selected property to True.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Sour Times by Portishead (Track 2 from the album: Dummy) What's This?
|
|

January 1st, 2005, 01:18 PM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar, thanks for your reply.
Would I be correct in saying that this routine should go in the "Page_Load"?
Because right now, I have the DDL contents assembled on the page_load.
Mark
|
|

January 1st, 2005, 03:32 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Mark,
Yes, that's probably the best place, although it depends on your specific requirements. Basically, you preselect the item when you bind your form / page to the data, which indeed usually takes place in the Page_Load event.
Make sure you do this only on the first hit to the page and not on PostBack as you'll overwrite the user's changes.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Working For The Man by PJ Harvey (Track 3 from the album: To Bring You My Love) What's This?
|
|

March 18th, 2005, 01:20 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, i'm sort of having the same problem and have attempted using this code before actually seeing it here without success. The problem i'm having is that i need to use it straight from the database or a variable for the FindByValue. When hard coded in, as per the example demonstrated in this topic, it works. However, when using a variable or database item, i get the following error:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
The code that generates this error is below. The line is bold is the problem area. The available item values available in the drop down list are "true" and "false", based on a boolean value from the database (MS SQL Server 2k). If you have any suggestions it will be greatly appreciated! Thanks :)
Sub SelectProject(s As Object, e As EventArgs)
objCmd = New SqlCommand("SELECT * FROM tblProjectData WHERE Project_ID=@ProjectID", objConn)
objCmd.Parameters.Add("@ProjectID", ddlProjects.SelectedItem.Value)
objConn.Open()
objRdr = objCmd.ExecuteReader()
While objRdr.Read()
intProjectID = objRdr.Item("Project_ID")
txtProjectCode.Text = objRdr.Item("Project_Code")
txtProjectName.Text = objRdr.Item("Project_Name")
txtProjectClient.Text = objRdr.Item("Client")
ddlProjectStatus.Items.FindByValue((objRdr.Item("S tatus")).ToString()).Selected = True
End While
|
|

March 18th, 2005, 03:19 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
What happens when you debug and diagnose the drop down list? Does it still contain the items?
Maybe ViewState is off and the control is not maintaining its state? Than the items no longer exist in the drop down...
One final reason could be that your data reader contains items that are not in the list. In that case, check for null first, before you try to set the Selected property....
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

March 18th, 2005, 03:32 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar, thanks for the quick reply. I have just worked out what the problem is, and it was a pretty silly error! it turns out that the List items are case sensitive. Me changing the value of the list items from "true" to "True" and "false" to "False" fixed it... weird, but all good now.
Thanks for your help, much appreciated :)
|
|
 |