selasedaniweb 0 Newbie Poster

The first paragraph of the code above is the loginview control that resides in my Masterpage.


the second paragraph is the login page.
where i inserted a login control and writes the code for user authentication

the third paragraph is the code that processes the user authentication and the redirection to the resultant page.

the problem am having is the user name and the logout text does not appear after the page has been redirected. where could i be going wrong?

<div class="loginView">
                    <asp:LoginView ID="MasterLoginView" runat="server">
                        <LoggedInTemplate>
                            Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /> 
                                <asp:Label ID="userNameLabel" runat="server" Text="Label"></asp:Label></span>!
                        [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/Logout.aspx"/> ]
                            <%--Welcome: 
                            <span class="bold"><asp:LoginName ID="MasterLoginName" runat="server" /> </span>!--%>                       
                        </LoggedInTemplate>
                        <AnonymousTemplate>
                            Welcome: Guest
                            [ <a href="~/Account/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
                        </AnonymousTemplate>
                        
                    </asp:LoginView>
                    <%--   [ <asp:LoginStatus ID="MasterLoginStatus" runat="server" LogoutAction="Redirect" LogoutPageUrl="~/Logout.aspx" /> ]  --%>

                </div>





<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 style="text-align:left; font-size:1.2em; color:White;">Account Information</legend>
                    <p style="text-align:left; font-size:1.2em; color:White;">
                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User ID:</asp:Label>
                        <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                             CssClass="failureNotification" ErrorMessage="User ID is required." ToolTip="User ID field is required." 
                             ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                    </p>
                    <p style="text-align:left; font-size:1.2em; color:White;">
                        <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 style="text-align:left; font-size:1.2em; color:White;">
                        <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" onclick="LoginButton_Click"/>
                </p>
            </div>
        </LayoutTemplate>
    </asp:Login>




string userIDStr, pwrdStr;
        
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            

            //assign textbox items to string objects
            userIDStr = LoginUser.UserName.ToString();
            pwrdStr = LoginUser.Password.ToString();

            //SQL connection string

            string strConn;
            strConn = WebConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ConnectionString;

            SqlConnection Conn = new SqlConnection(strConn);
            


            //SqlDataSource CSMDataSource = new SqlDataSource();
           // CSMDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ToString();

            
            //SQL select statement for comparison

            string sqlUserData;
            sqlUserData = "SELECT StaffID, StaffPassword, StaffFName, StaffLName, StaffType FROM Staffs";
            sqlUserData += " WHERE (StaffID ='" + userIDStr + "')";
            sqlUserData += " AND (StaffPassword ='" + pwrdStr + "')";

            SqlCommand com = new SqlCommand(sqlUserData, Conn);
            SqlDataReader rdr;
            string usrdesc;
            string lname;
            string fname;
            string staffname;

            try
            {
                
                //string CurrentData;
                //CurrentData = (string)com.ExecuteScalar();
                Conn.Open();
                rdr = com.ExecuteReader();
                rdr.Read();
                usrdesc = (string)rdr["StaffType"];
                fname = (string)rdr["StaffFName"];
                lname = (string)rdr["StaffLName"];
                staffname = lname.ToString() + " " + fname.ToString();
                LoginUser.UserName = staffname.ToString();
                rdr.Close();

                if (usrdesc.ToLower() == "administrator")
                {
                    
                    Response.Redirect("~/CaseAdmin.aspx", false);
                    
                 }
                 else if (usrdesc.ToLower() == "manager")
                 {
                     Response.Redirect("~/CaseManager.aspx", false);
                 }
                 else if (usrdesc.ToLower() == "investigator")
                 {
                     Response.Redirect("~/Investigator.aspx", false);
                 }
                 else
                 {
                     Response.Redirect("~/Default.aspx", false);
                 }               
                    
                
            }
            catch(Exception ex)
            {
                string script = "<script>alert('" + ex.Message + "');</script>";
            }
            finally
            {
                Conn.Close();
            }


        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.