-1

I have my login form and i tried to parameter the query for logging in user. But it seem to give an error that Login failed for user. My code is here. Please tell me what am i doing wrong here.

public void LoginUser()
{
    string UserNameFromHTML = Page.Request.Form["UserNameIput"];
    string UserPasswordFromHTML = Page.Request.Form["UserPasswordInput"];
    string QueryString = "SELECT User_Id, User_Name, User_Password FROM um_Personnel WHERE User_Name = @UserName and User_Password = @UserPassword";
    SqlCommand Command = new SqlCommand();
    Command.CommandText = QueryString;
    Command.Connection = ConnectionString;
    Command.Parameters.AddWithValue("@UserName", UserNameFromHTML);
    Command.Parameters.AddWithValue("@UserPassword", UserPasswordFromHTML);
    using (SqlDataAdapter Data_Adapter = new SqlDataAdapter(Command))
    {
        DataSet Data_Set = new DataSet();
        Data_Adapter.Fill(Data_Set);
        if (Data_Set.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("CMS/Dashboard.aspx");
        }
    }
}

and i am calling this function on my button onClick event as

<button type="submit" class="submit" onclick='<% LoginUser(); %>'>

THE COMPLETE HTML CODE IS HERE

<fieldset>
            <legend class="legend">User Login</legend>
            <div class="input">
                <input type="text" placeholder="Enter User Name" id="UserNameIput" required />
                <span><i class="fa fa-envelope-o"></i></span>
            </div>
            <div class="input">
                <input type="password" placeholder="Enter Password" id="UserPasswordInput" required />
                <span><i class="fa fa-lock"></i></span>
            </div>
            <button type="submit" class="submit" onclick='<% LoginUser(); %>'><i class="fa fa-long-arrow-right"></i></button>
        </fieldset>
adil sharif
  • 55
  • 1
  • 4
  • 14

2 Answers2

1

this part of code

<input type="text" placeholder="Enter User Name" id="UserNameIput" required />

did not contains the name property that is the one that return the parameter. To make it work, add the name (the id is not used from what I see), and make it as:

<input type="text" placeholder="Enter User Name" name="UserNameIput" required />

then you can get the input from code behind as:

Page.Request.Form["UserNameIput"];

Do the same for all input that you need to get the value on code behind, or use server controls. I ask you if you debug your code step by step, but apparently you did not do that and not check if you have anything on that values. So you may have more errors.

Debug your code step by step and check the parameters, check if the sql is running correctly - and improve your code.

Aristos
  • 66,005
  • 16
  • 114
  • 150
0

After you checked if the user has the right login and password, you'll need to actually Login the user.

public void LoginUser()
{
    string UserNameFromHTML = Page.Request.Form["UserNameIput"];
    string UserPasswordFromHTML = Page.Request.Form["UserPasswordInput"];
    string QueryString = "SELECT User_Id, User_Name, User_Password FROM um_Personnel WHERE User_Name = @UserName and User_Password = @UserPassword";
    SqlCommand Command = new SqlCommand();
    Command.CommandText = QueryString;
    Command.Connection = ConnectionString;
    Command.Parameters.AddWithValue("@UserName", UserNameFromHTML);
    Command.Parameters.AddWithValue("@UserPassword", UserPasswordFromHTML);
    using (SqlDataAdapter Data_Adapter = new SqlDataAdapter(Command))
    {
        DataSet Data_Set = new DataSet();
        Data_Adapter.Fill(Data_Set);
        if (Data_Set.Tables[0].Rows.Count > 0)
        {
            FormsAuthentication.RedirectFromLoginPage(UserNameFromHTML, true); //will return the user to the page who needs a user who is logged in            
        }
        else
        {
            Responce.Redirect("~/Home/Index/");
        }
    }
}

And in Web.config add this:

<authentication mode="Forms">
    <forms name=".ASPXFORMSDEMO" loginUrl="logon.aspx" protection="All" path="/" timeout="30" />
</authentication>

You can check this page for a basic start.

Stef Geysels
  • 1,023
  • 11
  • 27