Codeone Codeone - 2 months ago 12
ASP.NET (C#) Question

Login user's Name not displayed

I have developed an ASP.NET web application. The login page and logout functionality is working correctly. I have added a new method to display the logged in user's name. After adding this method I can't login to my application. I don't think i've added this method correctly.
Login
Can you please help?

Login.aspx

protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
DataTable dtUser = UserRegistration.GetUserByUserName(txtUserName.Text, txtPassword.Text);
if (dtUser.Rows.Count > 0)
{
lblSuccessMessage.Text = "Login Successful!";

oLoginData = txtUserName.Text;

Session["intUserId"] = dtUser.Rows[0]["intUserId"].ToString();
Session["DisplayName"] = dtUser.Rows[0]["DisplayName"].ToString();
Response.Redirect("~/WebForms/Home/Home.aspx");

}
else
{
lblErrorMessage.Text = "Incorrect User Name or Password";
txtUserName.BackColor = System.Drawing.Color.LavenderBlush;
txtPassword.BackColor = System.Drawing.Color.LavenderBlush;
return;
}
}
catch
{
lblErrorMessage.Text = "Incorrect User Name or Password";
txtUserName.BackColor = System.Drawing.Color.LavenderBlush;
txtPassword.BackColor = System.Drawing.Color.LavenderBlush;
return;
}
}
}





Site.Masters.cs

protected void Page_Load(object sender, EventArgs e)
{
if ((Session["intUserId"] == null))
{
FormsAuthentication.SignOut();
Response.Redirect("~/Login.aspx");
}
else
{
//lblLocation.Text = Session["LocationName"].ToString();
lblUser.Text = "Logged User :" + Session["DisplayName"].ToString();
}
}


GetUserByUserName

public static DataTable GetUserByUserName(string UserName, string Password)
{
DataTable dsResult = new DataTable();
try
{


String strConnString = ConfigurationManager.ConnectionStrings["TCDMSConnection"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand com = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();

con.Open();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "spUserValidation";

SqlParameter[] sqlParam = new SqlParameter[2];

sqlParam[0] = new SqlParameter("@Username", UserName);
sqlParam[1] = new SqlParameter("@Password", Password);

if (sqlParam != null)
{
com.Parameters.AddRange(sqlParam);
}

da.SelectCommand = com;
da.Fill(dsResult);

con.Close();

return dsResult;

}
catch (Exception ex)
{
throw (ex);
}




}


}


Image

Answer

Finally i found the solution, im not add correctly SP for DisplayName Col

AS
BEGIN

    SELECT 
        UserId,
        UserName,
        FullName,
        UserCatagoryId,
         DisplayName

    FROM UserRegistrations
    WHERE UserName = @Username AND Password = @Password


END
Comments