Solved

VS 2005 DetailsView test for null

Posted on 2013-12-04
6
179 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now