Last post Jul 01, 2010 08:22 AM by donpisci
Member
19 Points
176 Posts
Jun 27, 2010 12:30 PM|donpisci|LINK
Hi folks,
I'm getting the above error when I get to the following line:
string result = Convert.ToString(command.ExecuteScalar());
am really confused because everything I've read tells me that that line is correct!
Thanks in advance!
My code is as follows:
public bool Login(string userName, string password) { // read the coonection string from web.config string conString = ConfigurationManager.ConnectionStrings["MySqlDatabase"].ConnectionString; //Encrypt encrypt = new Encrypt(); //encrypt.EncodePassword(password); using (MySql.Data.MySqlClient.MySqlConnection mySqlConn = new MySql.Data.MySqlClient.MySqlConnection(conString)) { try { mySqlConn.Open(); MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand ("SELECT UserName FROM Users WHERE UserName = @UserName AND Password = MDS('@Password')"); command.Parameters.Add("@UserName", SqlDbType.Char).Value = userName; command.Parameters.Add("@Password", SqlDbType.VarChar).Value = password; command.Connection = mySqlConn; string result = Convert.ToString(command.ExecuteScalar()); if (string.IsNullOrEmpty(result)) { //invalid user/password , return flase return false; } else { // valid login return true; } } catch (Exception) { throw; } } } }
All-Star
124328 Points
10142 Posts
Jun 27, 2010 12:37 PM|SGWellens|LINK
As a general rule of thumb: NEVER nest function calls.
Try this:
Object Scalar = command.ExecuteScalar()
string result = Scalar.ToString();
Jun 27, 2010 12:51 PM|donpisci|LINK
Hi mate,
Thanks for that, I copied it exactly, but am getting the 'object reference not set to an instance of an object'.
I've also tried doing the following, but get the same error. I know it's simeple, but I can't for the life of me see what I doing wrong!
object scalar = new object()
scalar = command.ExecuteScalar()
Jun 27, 2010 01:12 PM|donpisci|LINK
I know you said it was bad, but I managed to get around the problem using a nested function and instead of having:
'command.Parameter.Add'
I've changed it to:
'command.Parameter.AddWithValue'
Thanks!
Jun 27, 2010 06:32 PM|SGWellens|LINK
Perhaps the function is returning null and it is being converted to zero. You may still have a problem.
Jul 01, 2010 08:22 AM|donpisci|LINK
Hi Steve,
Sorry for not getting back sooner....you're right, it is returning null. Will have to re- write me thinks.
Member
19 Points
176 Posts
Input string was not in a correct format.
Jun 27, 2010 12:30 PM|donpisci|LINK
Hi folks,
I'm getting the above error when I get to the following line:
string result = Convert.ToString(command.ExecuteScalar());
am really confused because everything I've read tells me that that line is correct!
Thanks in advance!
My code is as follows:
All-Star
124328 Points
10142 Posts
Re: Input string was not in a correct format.
Jun 27, 2010 12:37 PM|SGWellens|LINK
As a general rule of thumb: NEVER nest function calls.
Try this:
Object Scalar = command.ExecuteScalar()
string result = Scalar.ToString();
My blog
Member
19 Points
176 Posts
Re: Input string was not in a correct format.
Jun 27, 2010 12:51 PM|donpisci|LINK
Hi mate,
Thanks for that, I copied it exactly, but am getting the 'object reference not set to an instance of an object'.
I've also tried doing the following, but get the same error. I know it's simeple, but I can't for the life of me see what I doing wrong!
object scalar = new object()
scalar = command.ExecuteScalar()
string result = Scalar.ToString();
Member
19 Points
176 Posts
Re: Input string was not in a correct format.
Jun 27, 2010 01:12 PM|donpisci|LINK
I know you said it was bad, but I managed to get around the problem using a nested function and instead of having:
'command.Parameter.Add'
I've changed it to:
'command.Parameter.AddWithValue'
Thanks!
All-Star
124328 Points
10142 Posts
Re: Input string was not in a correct format.
Jun 27, 2010 06:32 PM|SGWellens|LINK
Perhaps the function is returning null and it is being converted to zero. You may still have a problem.
My blog
Member
19 Points
176 Posts
Re: Input string was not in a correct format.
Jul 01, 2010 08:22 AM|donpisci|LINK
Hi Steve,
Sorry for not getting back sooner....you're right, it is returning null. Will have to re- write me thinks.