C# casting question

G

Guest

I know this one is basic (and Thanks)
i have a function that calls a storedprocedure and i'm using the return
code for validation See Below

public Boolean fn_isVaildUser(string userid, string password)
{
// returns 0 invalid or 1 valid for a userid and password
string constr =
ConfigurationManager.ConnectionStrings["arlcapConnectionString"].ConnectionString;
SqlConnection sqlcon1 = new SqlConnection(constr);
SqlCommand sqlcmd1 = sqlcon1.CreateCommand();
sqlcmd1.CommandType = CommandType.StoredProcedure;
sqlcmd1.CommandText = "sp_GetUserLogin";
sqlcmd1.Parameters.AddWithValue("@szUserid", userid);
sqlcmd1.Parameters.AddWithValue("@szPassword", password);
SqlParameter isOK = sqlcmd1.Parameters.Add("@isOK", null);
isOK.SqlDbType = SqlDbType.Int;
isOK.Direction = ParameterDirection.ReturnValue;
sqlcon1.Open();
sqlcmd1.ExecuteNonQuery();
sqlcon1.Close();
sqlcmd1.Dispose();
sqlcon1 = null;
sqlcmd1 = null;
if ((Int32)isOK.Value == 0)
return false;
else
return true;
/// return (Boolean)isOK.Value;
}
The question has to do with the last commented out line.
I want to just want t ocast and return the "isOK.Value but i keep getting
errors.
ERROR: "Specified cast is not valid."
(It does work as is, but i'd perfer to do it what i consider correctly.)
 
S

Sherif Elmetainy

Your parameter type should be bit so that you can cast it to Boolean.
Otherwise you can use Convert.ToBoolean(isOK.Value)

Regards,
Sherif
 
G

Guest

Thank you,
I'm still getting used to C#, but i think in this case is was an issue of
missing the obvious!
--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes


Sherif Elmetainy said:
Your parameter type should be bit so that you can cast it to Boolean.
Otherwise you can use Convert.ToBoolean(isOK.Value)

Regards,
Sherif

WebBuilder451 said:
I know this one is basic (and Thanks)
i have a function that calls a storedprocedure and i'm using the return
code for validation See Below

public Boolean fn_isVaildUser(string userid, string password)
{
// returns 0 invalid or 1 valid for a userid and password
string constr =
ConfigurationManager.ConnectionStrings["arlcapConnectionString"].ConnectionString;
SqlConnection sqlcon1 = new SqlConnection(constr);
SqlCommand sqlcmd1 = sqlcon1.CreateCommand();
sqlcmd1.CommandType = CommandType.StoredProcedure;
sqlcmd1.CommandText = "sp_GetUserLogin";
sqlcmd1.Parameters.AddWithValue("@szUserid", userid);
sqlcmd1.Parameters.AddWithValue("@szPassword", password);
SqlParameter isOK = sqlcmd1.Parameters.Add("@isOK", null);
isOK.SqlDbType = SqlDbType.Int;
isOK.Direction = ParameterDirection.ReturnValue;
sqlcon1.Open();
sqlcmd1.ExecuteNonQuery();
sqlcon1.Close();
sqlcmd1.Dispose();
sqlcon1 = null;
sqlcmd1 = null;
if ((Int32)isOK.Value == 0)
return false;
else
return true;
/// return (Boolean)isOK.Value;
}
The question has to do with the last commented out line.
I want to just want t ocast and return the "isOK.Value but i keep getting
errors.
ERROR: "Specified cast is not valid."
(It does work as is, but i'd perfer to do it what i consider correctly.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top