Answered by:
how can i get the description of the user who is logged in at that moment using two different database table?

Question
-
User330911564 posted
tblUser tblDescription
ID | username | password descId | userID | description
----------------------------- 1 | 2 | this is for the test description
1 | admin | admin 2 | 1 | this is for the admin description
2 | test | test
so i have created two different tables in my sql database (tblUsers and tblDescription) and of course every user has an id
but what i want to do is when a user logs in ,
it will check the id of the user who is logged in and display the description in an asp.net label.
i've been able to create the login with formauthentication in c# but i can't figure out how to get the description of the user?
i've done a couple research as i'm new to database and it works on my query in ms sql but i still couldn't get it right in my c#
can i get some help , thanks in advance.
c#, database, sql statement, asp.net
Thursday, June 22, 2017 2:54 PM
Answers
-
User-707554951 posted
Hi tandohtakyie
Use the following code:
protected void Button1_Click(object sender, EventArgs e) { string username = txtUserName.Text; using (SqlConnection con = new SqlConnection()) { con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select * from tblUser where username='" + username + "'"; cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { if (sdr.HasRows) { while (sdr.Read()) { // get password and user ID from databse based on username string password = sdr["password "].ToString(); int id = Convert.ToInt16(sdr["ID"].ToString()); if (txtPassWord.Text == password) { using (SqlConnection conn= new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlCommand cmd2 = new SqlCommand()) { cmd2.Connection = conn; conn.Open(); cmd2.CommandText = "select * from tblDescription where userID=" + id; using (SqlDataReader dr = cmd2.ExecuteReader()) { if (dr.HasRows) { while (dr.Read()) { string description = dr["description"].ToString(); Response.Write(description); } } } } conn.Close(); } // FormsAuthentication.RedirectFromLoginPage(txtUserName.Text.ToString(), false); } } } else { //redirect to regiest page or input user name } } con.Close(); } } }
Best regard
Cathy
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 23, 2017 7:41 AM
All replies
-
User-707554951 posted
Hi tandohtakyie
Use the following code:
protected void Button1_Click(object sender, EventArgs e) { string username = txtUserName.Text; using (SqlConnection con = new SqlConnection()) { con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select * from tblUser where username='" + username + "'"; cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { if (sdr.HasRows) { while (sdr.Read()) { // get password and user ID from databse based on username string password = sdr["password "].ToString(); int id = Convert.ToInt16(sdr["ID"].ToString()); if (txtPassWord.Text == password) { using (SqlConnection conn= new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlCommand cmd2 = new SqlCommand()) { cmd2.Connection = conn; conn.Open(); cmd2.CommandText = "select * from tblDescription where userID=" + id; using (SqlDataReader dr = cmd2.ExecuteReader()) { if (dr.HasRows) { while (dr.Read()) { string description = dr["description"].ToString(); Response.Write(description); } } } } conn.Close(); } // FormsAuthentication.RedirectFromLoginPage(txtUserName.Text.ToString(), false); } } } else { //redirect to regiest page or input user name } } con.Close(); } } }
Best regard
Cathy
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 23, 2017 7:41 AM -
User330911564 posted
thank Cathy Zou
your code works perfect
i appriciate your effort
Friday, June 23, 2017 8:50 AM