Solved

VS 2005 DetailsView test for null

Posted on 2013-12-04
6
181 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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