Ponch Robles

Ponch Robles

  • NA
  • 11
  • 552

How to create buttons dynamically?

Mar 29 2017 9:29 PM
As I can create buttons automatically depending on the user who logs in, for example an administrator user is of type administrator and has all the privileges, then the admin user when login in the form should generate the privileges like buttons, how can it be done? 
My login code:
 
  private void button1_Click(object sender, EventArgs e) { if (txtUser.Text == "" || txtContrasenna.Text == "") { MessageBox.Show("Please provide UserName and Password"); return; } try { //Create SqlConnection SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand("Select * from Users where NameUser=@nameuser and Password=@password", con); cmd.Parameters.AddWithValue("@nameuser", txtUser.Text); cmd.Parameters.AddWithValue("@password", txtContrasenna.Text); con.Open(); SqlDataAdapter adapt = new SqlDataAdapter(cmd); DataTable ds = new DataTable(); adapt.Fill(ds); con.Close(); // int count = ds.Tables[0].Rows.Count; //If count is equal to 1, than show frmMain form if (ds.Rows.Count == 1) { switch (ds.Rows[0]["UserTypeId"] as int?) { case 1: { this.Hide(); Menus.MenuSuperUser ss = new Menus.MenuSuperUser(); ss.Show(); // MessageBox.Show("MAMALON"); break; } case 2: { this.Hide(); PantallasUsers.DashboardVendedor ss = new PantallasUsers.DashboardVendedor(); ss.Show(); break; } default: { MessageBox.Show("NOTBABY"); break; } } } else { MessageBox.Show("Algo esta mal, revisa tus datos."); } } catch (Exception ex) { MessageBox.Show(ex.Message); }

Answers (3)