Solved

Object reference not set to an instance of an object.

Posted on 2006-06-14
8
287 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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.
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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