Click here to Skip to main content
15,921,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guyz,
I am working on login form for my site and I wanna check if the user login is succsseful or failed to login and show it in label. So how can I do this? I tried to use the event of statment complete of selection statment but I faild . :( any help?
Posted
Updated 7-Sep-11 19:55pm
v2

if u are using aspnet membership tables
u could make it easy
use login control
& in the event of LoginError
set the LoginError.FailureText

or u could add it in desigin time
 
Share this answer
 
<pre lang="c#">
///// login validate /////////
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = default(SqlConnection);
SqlCommand cmd = default(SqlCommand);
SqlDataReader dr = default(SqlDataReader);
// Seession define
string uid = null;
uid = TextBox1.Text;
Session["Username"] = uid;

string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
con = new SqlConnection(constr);

con.Open();
cmd = new SqlCommand("select * from Reg", con);
dr = cmd.ExecuteReader();
string s = null;
string s1 = null;
while ((dr.Read()))
{
s = dr["Username"].ToString();
s1 = dr["Password"].ToString();

if (((TextBox1.Text == s.Trim()) & (TextBox2.Text == s1.Trim())))
{
Response.Redirect("Home.aspx");

}
Label3.Visible = true;

}
con.Close();
}</pre>
 
Share this answer
 
v3
You can use this code for the login control that will display message for you.

XML
<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
        <LayoutTemplate>
            <span class="failureNotification">
                <asp:Literal ID="FailureText" runat="server"></asp:Literal>
            </span>
            <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"
                 ValidationGroup="LoginUserValidationGroup"/>
            <div class="accountInfo">
                <fieldset class="login">
                    <legend>Account Information</legend>
                    <p>
                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
                        <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                             CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required."
                             ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                    </p>
                    <p>
                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                        <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                             CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."
                             ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                    </p>
                    <p>
                        <asp:CheckBox ID="RememberMe" runat="server"/>
                        <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
                    </p>
                </fieldset>
                <p class="submitButton">
                    <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/>
                </p>
            </div>
        </LayoutTemplate>
    </asp:Login>
 
Share this answer
 
Comments
Haitham tarek 8-Sep-11 1:27am    
thx for that its cool :) but i need to learn how make this notification if the password or email was wrong or not match my data in database ..
i did it like that :
int count = cmdchek.ExecuteNonQuery();
if (count == -1 )
lblres.Text = "Login Successfully";
else
lblres.Text = "Login Faild";
but thats not good i guess :/

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