Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 30th, 2004, 10:10 PM
Authorized User
 
Join Date: Jul 2004
Location: St. John\'s, NL, Canada.
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

  #2 (permalink)  
Old December 31st, 2004, 06:04 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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?
  #3 (permalink)  
Old January 1st, 2005, 01:18 PM
Authorized User
 
Join Date: Jul 2004
Location: St. John\'s, NL, Canada.
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

  #4 (permalink)  
Old January 1st, 2005, 03:32 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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?
  #5 (permalink)  
Old March 18th, 2005, 01:20 AM
Registered User
 
Join Date: Mar 2005
Location: Perth, Western Australia, Australia.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


  #6 (permalink)  
Old March 18th, 2005, 03:19 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.
  #7 (permalink)  
Old March 18th, 2005, 03:32 AM
Registered User
 
Join Date: Mar 2005
Location: Perth, Western Australia, Australia.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 :)


 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
drop down list ashu_gupta75 Excel VBA 1 October 15th, 2007 05:52 PM
Drop down list carumuga Classic ASP Basics 1 February 6th, 2007 02:37 PM
drop down list values based on another drop down noor ASP.NET 1.0 and 1.1 Basics 3 July 5th, 2005 09:57 AM
Drop Down List mrideout BOOK: Beginning ASP.NET 1.0 8 September 16th, 2004 07:11 AM



All times are GMT -4. The time now is 09:10 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.