Question

i'm try to match the data with the database and using dataadapter to fill my datatable. if matched, fill the datatable with dataadapter. if not match, show msg. but my username and password is matched with database, it still show msg. under debug mode, username and password is all pass through but not fill in the datatable.

using (OracleConnection conn = new OracleConnection())
{
    conn.ConnectionString = connStr;
    conn.Open();

    string sql = @"select user_id, password, status, role_id, email, contact_no, 
                   last_login_date, created_by, last_update_date, last_update_by
                   from users where user_id = :userID and password = :pwd";

    using (OracleCommand cmd = new OracleCommand())
    {
       cmd.Connection = conn;
       cmd.CommandText = sql;

       cmd.Parameters.Add("userID", OracleType.VarChar).Value = userID;

       cmd.Parameters.Add("pwd", OracleType.VarChar).Value = pwd;

       DataTable dt = new DataTable();
       OracleDataAdapter adapter = new OracleDataAdapter(cmd);
       adapter.Fill(dt);

       if (dt.Rows.Count <= 0)
       {
           msg = "Invalid Login ID or Password";
       }

       return dt;
    }
}

the dt.Rows.Count is 0. but I checked username and password is exactly same with the database.

Was it helpful?

Solution

SQL:

create procedure sp_authenticate
(
    @userId varchar(50),
    @pass varchar(50)
)
as
begin
    select user_id, password, status, role_id, email, contact_no, 
    last_login_date, created_by, last_update_date, last_update_by
    from users where user_id = @userid and password = @pass
end

C# code:

using (OracaleConnection con=new OracaleConnection())
{
    conn.ConnectionString = connStr;
    conn.Open();

    using (OracleCommand cmd = new OracleCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "sp_authenticate";  //name of your procedure
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@userid", OracleType.VarChar,50).value=userID;
        cmd.Parameters.Add("@password", OracleType.VarChar,50).value=pwd;

        DataTable dt = new DataTable();
        OracleDataAdapter adapter = new OracleDataAdapter(cmd);
        adapter.Fill(dt);

        if (dt.Rows.Count <= 0)
        {
            msg = "Invalid Login ID or Password"; }
        }

        return dt;
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top