Solved

VS 2005 DetailsView test for null

Posted on 2013-12-04
6
183 Views
Last Modified: 2013-12-04
I have a webform with a DetailsView where users can request badges for up to 5 visitors. Only one is required.

I need to create a database record (SQL 2005) and send an email based on user input.

The problem comes when there is only one name entered.

Then, for example, the visitorName2 is null. It's no problem for the database part of things

For the email message, however, I don't want to include any names that are left blank.

This is where I have trouble. I'm using the inserting event of the sqldatasource to check the value in the detailsview

If the value is not null, I want to append it to the email message. If it's null, then do nothing.
I thought maybe if I defined an Object variable, that might work, but it didn't
Here is the code that doesn't work:
    objValue = e.Command.Parameters("@visitorName2").Value
    If objValue IsNot DBNull.Value Then
      EMailBodyStringBuilder.Append("Visitor 2: " & e.Command.Parameters   ("@visitorName2").Value.ToString)
    End If

Open in new window


Any help would be appreciated.
0
Comment
Question by:lakhi
  • 3
  • 3
6 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 39696507
"The problem comes when there is only one name entered. "
What is the problem though? Is there an error message? if so, please provide the message and the line of code that throws the error.
0
 

Author Comment

by:lakhi
ID: 39696543
As soon as I hit the submit button to debug the form, I get "Object reference not set to an instance of an object" for this line:

EMailBodyStringBuilder.Append("Visitor 2: " & e.Command.Parameters("@visitorName2").Value.ToString)

If I add data to the visitorName2 field, I don't get the error.
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 430 total points
ID: 39696600
I'm guessing  that e.Command.Parameters("@visitorName2").value is nothing, not null so check for that instead:
IF Not e.Command.Parameters("@visitorName2").value is nothing then
EMailBodyStringBuilder.Append("Visitor 2: " & e.Command.Parameters   ("@visitorName2").Value.ToString)
    End If
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:lakhi
ID: 39696839
That was the problem!! Thank you so much!
0
 

Author Closing Comment

by:lakhi
ID: 39696846
Problem solved quickly and the answer was perfect
0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 39697129
Your welcome.  object reference... error means the object is nothing whereas if the value had been null, the exception would tell you that it can't convert null to string.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question