I am a high school student who's still pretty much a beginner in C#.
I am making a library management system (for books) that includes a database (sql local database in visual studio(?)) for users. I have a form wherein users can view the data they have input in the registration form (userID, name, username, course, section). The only problem is that it only displays the data of the first account created. No matter how many other accounts I create, it still only ever displays the first one. How do I make it so that it shows the data of the "current" user/account logged in?
I've tried slightly changing the code by changing
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from [tbl_accounts]";
into
string select = "Select * from [tbl_accounts]";
SqlCommand cmd = new SqlCommand(select, conn);
Although, I think they're basically the same. I don't really know what to do since the other solutions I've found are much more complex.
This is the code that I am using right now:
try
{
SqlConnection conn = new SqlConnection(@"[connection string]");
conn.Open();
string select = "Select * from [tbl_accounts]";
SqlCommand cmd = new SqlCommand(select, conn);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
materialLabel6.Text = dr["accountID"].ToString();
materialLabel7.Text = dr["username"].ToString();
materialLabel8.Text = dr["name"].ToString();
materialLabel9.Text = dr["strand"].ToString();
materialLabel10.Text = dr["section"].ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);}
}
The outcome that I would like to see is for example:
Users(table):
- PersonA
- PersonB
Currently Logged in: PersonB
[PERSONB'S DATA]
So it means that the form will only display the data of PersonB instead of PersonA's