Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi in my table only two columns are there one UserName and Password..
i want to validate this if UserName and Password is correct then it wil redirect to Welcome.aspx page or Invalid login and password..
can any one help ..???
Posted
Updated 23-Dec-11 19:10pm
v2
Comments
ythisbug 24-Dec-11 5:23am    
int iResults ;

iResults=Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
if ((iResults ==1))
{
lblMessage.Text = "User not Found";
}
else
{
Response.Redirect("Welcome.aspx");

}

this is my code..if i type user name and password correctly also it showing user not found..can any one solve this...??
ythisbug 24-Dec-11 6:04am    
cmd.ExecuteScalar()

can any one tel wats dis mean?
ythisbug 25-Dec-11 23:59pm    
btnlogin..
if (FormsAuthentication.Authenticate(this.txtUsername.Text, this.txtPwd.Text))
Response.Redirect("Welcome.aspx");
else
lblMessage.Text = "<br>Invalid Login..Please reenter....";

webconfig..
<authentication mode="Forms">
<forms name="Welcome" path="Welcome.aspx" loginUrl="Login.aspx" protection="All" timeout="30">

<credentials passwordformat="Clear">
<user name="zaheer" password="zaheer">
<user name="fahad" password="fahad">

</forms>

<authorization>
<allow users="*">




hi this is web.config code..how to retrieve from database table.actually i dont want to give username and password in web config.i want to retrieve username and password from sql table only..hw to do dat??

Use form authentication and give there default page as Welcome.aspx. Search on Google Form Authentication in Asp.net you will found many articles.
 
Share this answer
 
C#
C#
protected void ValidateUserInfo(string user, string pass)
{
  
    SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
    string sql = "SELECT * FROM TableName WHERE UserID = @username AND Password = @password";
    SqlCommand cmd = new SqlCommand(sql,connection);
    cmd.Parameters.AddWithValue("@username", user);
    cmd.Parameters.AddWithValue("@password", pass);
    connection.Open();
 
    DataTable dt = new DataTable();
    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    ad.Fill(dt);
    if (dt.Rows.Count > 0) { //check if the query returns any data
        //Valid Username and Password
        Response.Redirect("Default.aspx");
    }
    else
    {
        Response.Write("INVALID Username and Password, Try Again!");
    }
    connection.Close();   
}
protected void Button1_Click(object sender, EventArgs e)
{
  ValidateUserInfo(TextUserName.Text.Trim(), TextPassword.Text.Trim());
}


VB.NET

VB
Protected Sub ValidateUserInfo(ByVal user As String, ByVal pass As String)
   
    Dim connection As New SqlConnection("YOUR CONNECTION STRING HERE")
    Dim sql As String = "SELECT * FROM TableName WHERE UserID = @username AND Password = @password"
    Dim cmd As New SqlCommand(sql, connection)
    cmd.Parameters.AddWithValue("@username", user)
    cmd.Parameters.AddWithValue("@password", pass)
    connection.Open()
   
    Dim dt As New DataTable()
    Dim ad As New SqlDataAdapter(cmd)
    ad.Fill(dt)
    If dt.Rows.Count > 0 Then
        'check if the query returns any data
        Response.Redirect("Default.aspx")
    Else
        Response.Write("INVALID Username and Password, Try Again!")
    End If
    connection.Close()
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1_Click
    ValidateUserInfo(TextUserName.Text.Trim(), TextPassword.Text.Trim())
End Sub


That simple! Happy Coding!
 
Share this answer
 
Comments
[no name] 24-Dec-11 1:28am    
its work fine!
easy and simple to check username and password
lmoelleb 31-Jan-22 9:14am    
Yes, it is easy and simple as long as you understand that it should NEVER EVER be used for real passwords.
hi you can add this method to your login click button event

public void login()
{
string strLoginId;
        string strPassword;
        DataSet oDs = null;
        bool IsRemember = chkRememberMe.Checked;

//this is class file where you will be write your code for get the information to database
   CityBusinessLogics.AdminLogics oAdmin = new CityBusinessLogics.AdminLogics();
        strLoginId = txtUsername.Text.Trim();
        strPassword = txtPassword.Text.Trim();
        HttpCookie OnlineCookie = new HttpCookie("OnlineCookie");

        if (Page.IsValid)
        {
            try
            {
                oDs = oAdmin.ValidateAdminLogin(strLoginId, strPassword);
                if (oDs.Tables[0].Rows.Count <= 0)
                {
                    lblMessage.Text = "Wrong LoginId or Password !!";
                    lblMessage.Visible = true;
                }
                else if (IsRemember)
                {
                    string EnUserName, EnPwd;
                    EnUserName = oAdmin.base64Encode(strLoginId);
                    EnPwd = oAdmin.base64Encode(strPassword);

                    OnlineCookie.Values.Add("UserName", EnUserName);
                    OnlineCookie.Values.Add("Password", EnPwd);
                   

                    Response.Cookies.Add(OnlineCookie);
                    OnlineCookie.Expires = DateTime.Now.AddDays(15);

                    Session["Admin"] = strLoginId;
                   
                 if (Session["Admin"].ToString() == "admin")
                       Response.Redirect("AdminChangePassword.aspx");
                   else
                        Response.Redirect("RoleTask.aspx");

                }
                else
                {
                    //OnlineCookie.Values.Add("UserName",string .Empty);
                    //OnlineCookie.Values.Add("Password", string.Empty);
                    //OnlineCookie.Values.Add("Roles", string.Empty);
                    //Response.Cookies.Add(OnlineCookie);
                    //OnlineCookie.Expires = DateTime.Now.AddMinutes(5);
                    Session["Admin"] = strLoginId;
                   // Session["Roles"] = oDs.Tables[0].Rows[0]["RoleIds"].ToString();
                  if (Session["Admin"].ToString() == "admin")
                      Response.Redirect("AdminChangePassword.aspx");
                   else
                        Response.Redirect("RoleTask.aspx");
                }

            }
            catch
            {
                lblMessage.Text = "Login failed.";
                lblMessage.Visible = true;
            }
        }


I hope that this will be helpfull for you
thank you
 
Share this answer
 
VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1_Click
  result=  ValidateUser(TextUserName.Text, TextPassword.Text)
if result > 0 then
response.redirect("validpage.aspx")
endif
End Sub
public function validuser(byval user as string,byval pwd as string)
Dim connection As New SqlConnection("YOUR CONNECTION STRING HERE")
dim result as integer
    Dim sql As String = "SELECT count(*)  from TableName WHERE UserID = @username AND Password = @password"
    Dim cmd As New SqlCommand(sql, connection)
with cmd
   Parameters.AddWithValue("@username", user)
   Parameters.AddWithValue("@password", pass)
result=.executescalar()
if result > 0 then
return result
end if
end with
return 0
end function 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900