Solved

VS 2005 DetailsView test for null

Posted on 2013-12-04
6
185 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

685 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