Solved

Object reference not set to an instance of an object.

Posted on 2006-06-14
8
279 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
  • 4
  • 4
8 Comments
 
LVL 28

Expert Comment

by:ciuly
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:ciuly
ID: 16902643
what is the line with error?
0
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 28

Accepted Solution

by:
ciuly earned 250 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:ciuly
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

10 Experts available now in Live!

Get 1:1 Help Now