Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Object reference not set to an instance of an object.

Posted on 2006-06-14
8
Medium Priority
?
295 Views
Last Modified: 2012-06-27
Hello,

I have a stredproc which returns an email address if it finds in the tabel. I use this returned email and to insert records to another tabel and  if it doesn't find a macthing record i want to be able to give a message to the user who is using the app that there is no macthing record foun . however when it doesn't find one it says.

System.NullReferenceException: Object reference not set to an instance of an object.

how can i handle that?

thanks
0
Comment
Question by:jemigossaye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 16902484
you are probably using the returned email and since it is null, it throws that exception. do an
if (retEmail != null)
  showmessage;
else
  do whatever

If you can't figure it out, post that part of the code
0
 

Author Comment

by:jemigossaye
ID: 16902619
here is the code. I tried what u suggested but no change

 SqlCommand lo = new SqlCommand("UserGetEmail", conn);
                    lo.CommandType = CommandType.StoredProcedure;
                    lo.Parameters.Add("@Email", SqlDbType.NVarChar, 120).Value = alternateLogin;
                    conn.Open();
                    string LoEmail = lo.ExecuteScalar().ToString();
               
                    if (LoEmail != alternateLogin)
                  {
                        string LoInsert = ("INSERT INTO mms.dbo.user_inf (UserName,PasswordHint,UserAlternateLogin)" +
                             "Select Top 1 l.lo_login,'Email',l.email_address from INTRANET.dbo.lo_information l where l.email_address='" + (alternateLogin) + "' select scope_identity()");
                        SqlCommand LoCommand = new SqlCommand(LoInsert, conn);
 }
                    else
                    {
                        DisplayMessage.Show("There was no mathcing email address in the data base. Make sure you have inserted the correct one");
                    }
0
 
LVL 28

Expert Comment

by:2266180
ID: 16902643
what is the line with error?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jemigossaye
ID: 16902699
the line error is caused here :
string LoEmail = lo.ExecuteScalar().ToString(); when there is no email found
saying that Object reference not set to an instance of an object.
0
 
LVL 28

Accepted Solution

by:
2266180 earned 1000 total points
ID: 16902715
I see. replace that line with:
  string LoEmail;
  theneededtypehere obj = lo.ExecuteScalar();
  if (obj != null)
    LoEmail = obj.ToString();
  else
    LoEmail = null;
0
 

Author Comment

by:jemigossaye
ID: 16902730
what do u mean the need type here like the class it self
0
 
LVL 28

Expert Comment

by:2266180
ID: 16902744
I mean the class of "lo.ExecuteScalar()". The function returns a reference to a certain class; probably object. I am not sure and don't have an IDE handy to see.
0
 

Author Comment

by:jemigossaye
ID: 16902805
thanks, it does retrun an object.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 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