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.NullReferenceExcept ion: Object reference not set to an instance of an object.
how can i handle that?
thanks
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.NullReferenceExcept
how can i handle that?
thanks
ASKER
here is the code. I tried what u suggested but no change
SqlCommand lo = new SqlCommand("UserGetEmail", conn);
lo.CommandType = CommandType.StoredProcedur e;
lo.Parameters.Add("@Email" , SqlDbType.NVarChar, 120).Value = alternateLogin;
conn.Open();
string LoEmail = lo.ExecuteScalar().ToStrin g();
if (LoEmail != alternateLogin)
{
string LoInsert = ("INSERT INTO mms.dbo.user_inf (UserName,PasswordHint,Use rAlternate Login)" +
"Select Top 1 l.lo_login,'Email',l.email _address from INTRANET.dbo.lo_informatio n 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");
}
SqlCommand lo = new SqlCommand("UserGetEmail",
lo.CommandType = CommandType.StoredProcedur
lo.Parameters.Add("@Email"
conn.Open();
string LoEmail = lo.ExecuteScalar().ToStrin
if (LoEmail != alternateLogin)
{
string LoInsert = ("INSERT INTO mms.dbo.user_inf (UserName,PasswordHint,Use
"Select Top 1 l.lo_login,'Email',l.email
SqlCommand LoCommand = new SqlCommand(LoInsert, conn);
}
else
{
DisplayMessage.Show("There
}
what is the line with error?
ASKER
the line error is caused here :
string LoEmail = lo.ExecuteScalar().ToStrin g(); when there is no email found
saying that Object reference not set to an instance of an object.
string LoEmail = lo.ExecuteScalar().ToStrin
saying that Object reference not set to an instance of an object.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
thanks, it does retrun an object.
if (retEmail != null)
showmessage;
else
do whatever
If you can't figure it out, post that part of the code