Solved

VS 2005 DetailsView test for null

Posted on 2013-12-04
6
180 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 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

12 Experts available now in Live!

Get 1:1 Help Now