Solved

Object reference not set to an instance of an object.

Posted on 2006-06-14
8
281 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: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
 

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
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.

 
LVL 28

Accepted Solution

by:
2266180 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: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

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

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

16 Experts available now in Live!

Get 1:1 Help Now