CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2004
    Posts
    6

    Please help about sqlparameter

    i am using sql parameters for authenticat login and password but the porble is here an error ocurs "Specific casting not valid"
    see the code


    public bool Authenticate(string user, string pass)
    {
    bool authenticated = false;

    SqlConnection myconnection = new SqlConnection();
    myconnection.ConnectionString = "Persist Security Info=False;User Id = pcms;Password = pcms;Database=ImportPCMS;Data Source = Vilas;";
    myconnection.Open();
    SqlCommand cmd = new SqlCommand("Select Username,Password from muser",myconnection);

    try
    {

    SqlParameter myUser = new SqlParameter("@Username",SqlDbType.VarChar, 15);
    myUser.Value = user.Trim();
    cmd.Parameters.Add(myUser);

    SqlParameter myPassword = new SqlParameter("@Password",SqlDbType.VarChar, 15);
    myPassword.Value = pass.Trim();
    cmd.Parameters.Add(myPassword);

    SqlParameter IsValid = new SqlParameter("@IsValid",SqlDbType.Int);
    IsValid.Direction = ParameterDirection.Output;
    cmd.Parameters.Add(IsValid);

    cmd.ExecuteNonQuery();


    if ((int)Isvalid.value ==1)


    authenticated =true;

    }

    catch(Exception E)
    {
    Response.Write(E.Message);

    }if ((int)Isvalid.value ==1)

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    1) Which line is giving the error?

    2) What is the table schema?
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Dec 2002
    Location
    Birmingham, AL
    Posts
    82
    The error's at
    Code:
    if ((int)Isvalid.value ==1)
    because you can't cast directly from string to int.
    Code:
    if(System.Convert.ToInt64(IsValid.Value) == 1)

  4. #4
    Join Date
    Mar 2004
    Posts
    6
    ya ,
    but Isvalid.value returns a DBNull ?
    why?
    can u solve this?

  5. #5
    Join Date
    Mar 2004
    Posts
    6
    ya ,
    but Isvalid.value returns a DBNull ?
    why?
    can u solve this?

    and there is no schema define for it

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    If you allow nulls in the DB (required=NO) then you will get a DBNull when you read it.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured