Click here to Skip to main content
14,545,215 members
Rate this:
Please Sign up or sign in to vote.
See more:
my code for the login page
PLZ help me to display the login user name and also enable the logout link

Simple Login page

    <html xmlns="http://www.w3.org/1999/xhtml">
<br />
<head>
 <h2>
        Log In
    </h2>
    <p>
        Please enter your username and password.
        <asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="False" 

            ForeColor="Blue" NavigateUrl="~/register.aspx">Register if you don't have an account.
    </p>
<div class="accountInfo">
                <fieldset class="login">
                    <legend>Account Information</legend>
<title>Login Form</title>
</head>
<body>
<div>
<table>
<tr>
<td>
Username:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"/>
<asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPWD" runat="server" TextMode="Password"/>
<asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</body>
</fieldset></div>
</html>

site master page
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="True">
    <anonymoustemplate>
        [ <a href="~/Lg1.aspx" ID="HeadLoginStatus"  runat="server">Member Log In ]
    </anonymoustemplate>
    <loggedintemplate>
        <asp:LoginName ID="LoginName1" FormatString="Welcome {0}!" runat="server" />
        [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
    </loggedintemplate>


CS file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Lg1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from User_information where UserName =@username and Password=@password",con);
cmd.Parameters.AddWithValue("@username", txtUserName.Text);
cmd.Parameters.AddWithValue("@password", txtPWD.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0)
{
Response.Redirect("Default.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
    }
Posted
Updated 27-Apr-20 4:21am
v2
Comments
Sandeep Mewara 14-Mar-13 8:52am
   
Your question is not quite clear. Can you please rephrase and share what is the issue?
programmerbreakkid 14-Mar-13 9:51am
   
when clicked on login it logs in the user and redirects the user to the home page but the user name s not displayed and also the login button stays the same login it should actually be logout
Richard C Bishop 14-Mar-13 8:53am
   
You need to ask your question in the body of the thread and not in the header line. We cannot read what it says completely which will reduce the chances of you getting help.
programmerbreakkid 14-Mar-13 9:53am
   
when clicked on login it logs in the user after validating from sql server and redirects the user to the home page but the user name s not displayed on the page and also the login button stays the same login it will not change to logout... only if logout is enabled user can lougout
CHill60 14-Mar-13 10:17am
   
Are you saying that the user name and logout button should appear on the home page or are you loading the login page again?
programmerbreakkid 14-Mar-13 12:16pm
   
s the first part the username and the logout button should appear but that's not happening...
CHill60 14-Mar-13 13:35pm
   
How are you trying to display the user name and logout button on the home page? Are you passing them over in Session?
programmerbreakkid 14-Mar-13 14:04pm
   
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="True">
<anonymoustemplate>
[ <a href="~/Lg1.aspx" ID="HeadLoginStatus" runat="server">Member Log In ]

<loggedintemplate>
<asp:LoginName ID="LoginName1" FormatString="Welcome {0}!" runat="server" />
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]



this the template that s in VisualStudio 2010 by default masterpage has the login and wneh logged in after registration it will display usernameand also logout option but it has no sql connectivity the user name is stored in the db file in the APP_Data of the application......instad of using that mdf file of the app i wanted the information in dtabase of my own ...what i did was i created my own data base , created userlogin form and registration form and established a new sql connection,i am able to register and the information is getting stored in my database and when logging in with the same information it takes me to the page which i have set after validating the username and password every thing works fine but that login link above the page still stays login and logout is not enabled

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

your browser catch the page so you can do this . .

1:: Response.Cache.SetCacheability(HttpCacheability.NoCache); 'put this line in all page load event'

2 :: you have to reload page after login and logut

try this may work for you
   
v2
Comments
programmerbreakkid 14-Mar-13 9:48am
   
when clicked on login it logs in the user and redirects the user to the home page but the user name s not displayed and also the login button stays the same login it should actually be logout
Maciej Los 17-Mar-13 13:54pm
   
Comment from Archit Patel:
you have to give whole code than i can give you good ans .. :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100