srk1982
asked on
c# - Error -> Object reference not set to an instance of an object.
Hi experts,
I am using C# and Mysql in my project. I have pasted my sp and C# code in the below box.
I am returning a Int value from my SP. From c# i have to get that value, which determines whether the user has logged in or not.
My sp works fine, when i executed it in my Mysql query browser and it also returns a perfect value.
Problem is with my c# code to retrieve that value.
I am getting this error :
Object reference not set to an instance of an object.
in the line "doesExist = (int)cmd.ExecuteScalar();"
[Please refer the code.]
Please help me on this. Its very URGENT....
I am using C# and Mysql in my project. I have pasted my sp and C# code in the below box.
I am returning a Int value from my SP. From c# i have to get that value, which determines whether the user has logged in or not.
My sp works fine, when i executed it in my Mysql query browser and it also returns a perfect value.
Problem is with my c# code to retrieve that value.
I am getting this error :
Object reference not set to an instance of an object.
in the line "doesExist = (int)cmd.ExecuteScalar();"
[Please refer the code.]
Please help me on this. Its very URGENT....
SP :
-------------------------------------------------------------------------------
DELIMITER $$
DROP PROCEDURE IF EXISTS `demo`.`sp_LoginCheck`$$
create PROCEDURE `demo`.`sp_LoginCheck`(
IN UserName1 varchar(50),
IN Password1 varchar(50),
OUT DoesExist int)
BEGIN
declare userCount INT default 0;
SET DoesExist = 0; -- set to false initially, we'll assume for now it doesn't exist
SELECT COUNT(Uid) INTO userCount
FROM tbl_Users
WHERE username = username1 and password=password1;
IF userCount > 0 THEN
SET DoesExist = 1; -- more then one returned, return true
END IF;
END$$
DELIMITER ;
-------------------------------------------------------------------------------
C# :
-------------------------------------------------------------------------------
public static int CheckLogin_Instructor(string UserName, string Password)
{
int doesExist = 0;
try
{
MySqlConnection myConn = new MySqlConnection(ConfigurationSettings.AppSettings["Con"]);
MySqlCommand cmd = new MySqlCommand("sp_LoginCheck", myConn);
cmd.CommandType = CommandType.StoredProcedure;
MySqlParameter param = new MySqlParameter();
param = cmd.Parameters.Add("@UserName1", MySqlDbType.VarChar, 50);
param.Value = UserName;
param = cmd.Parameters.Add("@Password1", MySqlDbType.VarChar, 50);
param.Value = Password;
param = cmd.Parameters.Add("@DoesExist", MySqlDbType.Int32);
param.Direction = ParameterDirection.Output;
myConn.Open();
doesExist = (int)cmd.ExecuteScalar();
myConn.Close();
}
catch (Exception ex)
{
ExceptionHandler.writeToLogFile(ex.StackTrace.ToString());
}
return doesExist;
}
-------------------------------------------------------------------------------
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi needo_jee,
If i use your both idea i am getting the following error :
System.ArgumentException: Parameter 'string' not found in the collection.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi guys,
I changed the way i am doing. I am getting the value in a dataset and accesing that in my code.
Thanks for assisting me... I am sharing the points with all the 3 guys assisted me.
Thanks again.
I changed the way i am doing. I am getting the value in a dataset and accesing that in my code.
Thanks for assisting me... I am sharing the points with all the 3 guys assisted me.
Thanks again.
ASKER
There are 2 input parameters Username and Password.
They are not null.
The error is null reference exception, "Object reference not set to an instance of an object." which means that the return value is not coming from the SP [I guess]
Because the way i am executing the SP in mySql is
--------------------------
call sp_LoginCheck ('srk2','pwd2',@DoesExist)
SELECT @DoesExist
--------------------------
I think there is something to do with SP !!!
Correct me if i am wrong. I am a newbie for Mysql and C#.