0

I have a login form and modified some code from another source to make it work for my situation. I am getting the error when the login is not valid because the number is empty or 'infinite'.

public int Validate_Login(String Username, String Password)
{

    //SqlConnection con = new SqlConnection(@"User id=judgment_day;Password=Kit$hen;Server=216.40.232.82, 2153;Database=new_judgment-system");
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CityCollectionCSharp.Properties.Settings.newCityCollectionConnectionString"].ConnectionString);
    SqlCommand cmdselect = new SqlCommand();
    cmdselect.CommandType = CommandType.StoredProcedure;
    cmdselect.CommandText = "[dbo].[prcLoginv]";
    cmdselect.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = Username;
    cmdselect.Parameters.Add("@UPassword", SqlDbType.VarChar, 50).Value = Password;
    cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4);
    cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;
    cmdselect.Connection = con;
    int Results = 0;

    try
    {
        con.Open();
        cmdselect.ExecuteNonQuery();
        Results = (int)cmdselect.Parameters["@OutRes"].Value;
    }
    catch (SqlException ex)
    {
        lblMessage.Text = ex.Message;
    }
    finally
    {
        cmdselect.Dispose();

        if (con != null)
        {
            con.Close();
        }
    }
    return Results;
}

The error occurs on this line:

Results = (int)cmdselect.Parameters["@OutRes"].Value;

How would I go about error handling that so when it is not a valid integer that it will trigger the invalid login which the code is here:

protected void btnlogin_Click(object sender, EventArgs e)
{
    int Results = 0;


    if (txtUsername.Text != null && txtPassword.Text != null)
    {
        Results = Validate_Login(txtUsername.Text, txtPassword.Text);

        if (Results > 0)
        {
            lblMessage.Text = "Login is Good";

            GlobalVariables.Globals.SetGlobalInt(Results);
            lblMessage.Text = "'" + GlobalVariables.Globals.GlobalInt + "'";
            this.Hide();
            frmSwitch frm = new frmSwitch();
            frm.Show();   
        }
        else
        {
            lblMessage.Text = "Invalid Login";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }
    else
    {
        lblMessage.Text = "Please make sure that the username and the password is Correct";
    }
}

2 Answers 2

1

You seem to have majority of the code in place. I think you are not understanding how exceptions are handled - Read up on Exception Handling and you should be able to follow the example below.

As you do not specify the exact Exception being raised, I have added a general Excepion handler.

    try
    {
        con.Open();
        cmdselect.ExecuteNonQuery();
        // Check for null values
        if (cmdselect.Parameters["@OutRes"].Value != DBNull.Value)
        {
           Results = (int)cmdselect.Parameters["@OutRes"].Value;
        }
    }
    catch (SqlException ex)
    {
        lblMessage.Text = ex.Message;
    }
    catch (Exception generalEx)
    {
        // Do something with the error - Just displaying for now
        lblMessage.Text = ex.Message;
    }
    finally
    {
        cmdselect.Dispose();

        if (con != null)
        {
            con.Close();
        }
    }

The code is just for illustration, edit as required.

1
  • Thanks Kami. I think I was being lazy and forgot the if statement. This was originally set up to be 0 or 1 but I needed to pass the actual to other parts of my software and missed a major part. Thanks!
    – korrowan
    Dec 3, 2012 at 18:56
0

Try this:

try
{
    con.Open();
    cmdselect.ExecuteNonQuery();
    object o = (int)cmdselect.Parameters["@OutRes"].Value;
    if (o == DBNull.Value)
        return 0;
    return (int)o;
}
catch (SqlException ex)
{
    lblMessage.Text = ex.Message;
}
finally
....

and the finally block takes care of the resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.