Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics 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
  #11 (permalink)  
Old December 1st, 2005, 05:46 PM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, there is only a "null" value. I want to replace the "null" value with one from the DropDownList.

  #12 (permalink)  
Old December 1st, 2005, 06:28 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right, that explains a lot.

null is not a valid value in the DropDownList so you can't use that as a value for the SelectedValue property.

You should implement some code for the DataBound event of the DetailsView control. Inside that event handler, get a reference to the View's DataItem which by default is a DataRowView when you're using a default SqlDataSource. Then get the value from the column in the DataRowView, see if it's not null and see if you can find it in the DropDownList. The following should work:
Code:
Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) _
          Handles DetailsView1.DataBound
    Select Case DetailsView1.CurrentMode
      Case DetailsViewMode.Edit
        Dim myDataRowView As DataRowView = DetailsView1.DataItem
        If myDataRowView("Description") IsNot DBNull.Value Then
          Dim myDropDownList As DropDownList = _ 
         CType(DetailsView1.FindControl("DropDownList1"), DropDownList)
          If myDropDownList.Items.FindByValue(myDataRowView("Description")) _
         IsNot Nothing Then
            myDropDownList.Items.FindByValue(myDataRowView("Description")).Selected = True
          End If
        End If
    End Select
  End Sub
  There might be more efficient ways to do this though....
They say about .NET that it makes difficult things easy, and easy things difficult. I don't think that has changed much for .NET 2.0.... ;)


Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Here To Stay by KoRn (Track 1 from the album: Untouchables) What's This?
  #13 (permalink)  
Old December 1st, 2005, 06:39 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Oh, BTW, probably a *lot* easier is this:
<asp:DropDownList .....>
  <asp:ListItem Value="">Please select an color</asp:ListItem>
</asp:DropDownList>

Just insert a default item in the drop-down list that has a value of "". Then use a RequiredFieldValidator to force the user to make a new selection when editing an existing item that has a null value in the database.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: I Am The Owl by Dead Kennedys (Track 11 from the album: Plastic Surgery Disasters + In God We Trust) What's This?
  #14 (permalink)  
Old December 1st, 2005, 06:42 PM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar, I'm at work right now until 3 am, so I can't try the code until I get home. I hope it works and will post whether it does or not. Where do I learn about writing code like that? Would a VB book be a solution?

Thanks for all your help and I'll let you know if it works!

Greg

  #15 (permalink)  
Old December 1st, 2005, 06:52 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I am working on a book about ASP.NET 2,0 right now, scheduled for release in March ;)

If you can't wait that long, I think Professional ASP.NET 2.0 should cover topics like these. Beware: I haven't read nor seen that book yet, so I can't guarantee it covers these kind of topics.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: John Finn's Wife by Nick Cave & the Bad Seeds (Track 7 from the album: Henry's Dream) What's This?
  #16 (permalink)  
Old December 11th, 2005, 01:49 PM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey, I finally figured it out! I didn't have to implement any code. In the UpdateParameters collection for the Parameter "Color", I added the line: ConvertEmptyStringToNull="false" and it works like a champ! NO CODE to make it work. Thanks for the help.

Greg

  #17 (permalink)  
Old August 10th, 2006, 09:05 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Guy's

I am not sure if your are interacting with a SQL server 2005 Express database like I was but I got the same error it was because I had by Data Types as nchar(20) which is not a variable when it should have been varchar(20) or nvarchar(20) which every you need heres a link for it

http://www.databasejournal.com/featu...e.phpr/2212141

Lookly I identified the problem quick, its a stupid mistake and considering I did hell of a lot of database modules at uni I should shoot myself!

  #18 (permalink)  
Old June 7th, 2007, 04:54 PM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am also pulling out my hair with this!!


Author, SharpDeveloper.NET




Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored Procedure not saving to database rdove84 SQL Server 2000 1 January 22nd, 2008 12:32 AM
How to Set the Selection in Dropdownlist Control akhilhp ASP.NET 2.0 Basics 3 May 27th, 2007 07:13 AM
data selection and mapping with dropdownlist gagansharma7 ASP.NET 1.0 and 1.1 Basics 0 June 27th, 2006 11:41 AM
Saving Word file into Database melvik SQL Server 2000 4 November 28th, 2005 09:12 AM
Saving datatables into database argeam ADO.NET 1 June 1st, 2004 08:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.