Link to home
Start Free TrialLog in
Avatar of jemigossaye
jemigossaye

asked on

Object reference not set to an instance of an object.

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
Avatar of 2266180
2266180
Flag of United States of America image

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
Avatar of jemigossaye
jemigossaye

ASKER

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");
                    }
what is the line with error?
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.
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what do u mean the need type here like the class it self
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.
thanks, it does retrun an object.