I’m new to programming and using SQL Server Compact and I’m doing this personal project to learn, so any assistance is greatly appreciated.
I am trying to hide the button in the Main Form depending on the user type account that successfully logging.
In my database i have 3 columns which are username, password and type - (this is what I am trying to access for this project).
I figured that to get the effect that I want I would have to deal with the Main_load so this is where I placed this code:
private void Main_Load(object sender, EventArgs e)
{
admincontrolbtn.Visible = false;
conn = new SqlCeConnection("Data Source=Users.sdf; Persist Security Info = False");
comm = new SqlCeCommand("Select * from [user]", conn);
reader = comm.ExecuteReader();
while (reader.Read())
{
try
{
conn.Open();
if (reader.GetString(2) == "Administrator")
{
admincontrolbtn.Visible = true;
}
else
{
admincontrolbtn.Visible = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
}
When I built the code no error was found. I thought that I was on the right track. But when I logged in using a credential that has a User" type, the button was still visible.
I have already tried to debug the code by putting a break right on the Main_load itself but when i run it and log in, it goes to display the Main page ignoring the Main_load altogether.
I have also tried to set the visibility of the button to false it the properties but when the main page loads, even using the Administrator type of credential, the button stays hidden.
Was the syntax of the code wrong? and/or maybe my way of thinking that Main_load is my target wrong?
Normally when the user logs in you make a user object that holds all relevant information for that user. Then later you won’t have to access the database every time you want to know something about your user.
Your command should be different in that you only want information on one user: