Hi,
I'm trying to create a new user registration page in ASP.NET. I'm stuck with this error "Object reference not set to an instance of an object." and i couldn't figure it out where I'm going wrong
the try block code is here
try
{
SqlConnection objConnection = null;
SqlCommand objCommand = null;
objConnection = GetConnection();
objConnection.Open();
objCommand = GetCommandobj(objConnection);
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.CommandText = "CheckforDuplicates";
SqlParameter ObjParam1 = new SqlParameter("@Username", SqlDbType.NVarChar);
ObjParam1.Value = UserName;
SqlParameter ObjParam2 = new SqlParameter("@Firstname", SqlDbType.NVarChar);
ObjParam2.Value = strFname;
SqlParameter ObjParam3 = new SqlParameter("@Lastname", SqlDbType.NVarChar);
ObjParam3.Value = strLname;
SqlParameter ObjParam4 = new SqlParameter("@Emailadd", SqlDbType.NVarChar);
ObjParam4.Value = stremailadd;
SqlParameter ObjReturnParam = new SqlParameter("@Duplicates", SqlDbType.Int);
ObjReturnParam.Direction = ParameterDirection.ReturnValue;
objCommand.ExecuteNonQuery();
if (Int32.Parse(ObjReturnParam.Value.ToString()) > 0)
{
Response.Write("UserName already exists or you are already a registered user!");
return false;
}
else
{
return true;
}
objConnection.Close();
this is the line where it shows the error:
if (Int32.Parse(ObjReturnParam.Value.ToString()) > 0)
Any help and suggestion is appreciated.
THanks
Loading
Pasan RatnayakePosted Oct 7, 2010, 1:36 AM
Is it just me gone blind or you did not add the parameters to your SqlCommand?? Or did you not include the specific code here?? So obviously, you will have a null value in "ObjReturnParam.Value" and you will get an error when you try to convert it to string.
Hope this helps you resolve your error.
Regards
Manikavelu VelayuthamPosted Oct 7, 2010, 1:19 AM
Befor using the ObjReturnParam, just check for not null.
Your problem will be solved.
if (ObjReturnParam !=null && ObjReturnParam.Value !=null)
{
if (Int32.Parse(ObjReturnParam.Value.ToString()) > 0)
{
Response.Write("UserName already exists or you are already a registered user!");
return false;
}
else
{
return true;
}
}
your code should be replaced as above.