Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. static string serverName;
  2. MySqlCommand command;
  3. public static string connString = @"server=" + serverName + "; Database=database; username=username; password=password";
  4.  
  5. // this is the method
  6. public void fill_grid(string query)
  7. try
  8. {
  9.  
  10. dataGridView1.DataSource = null;
  11. MySqlConnection myconn = new MySqlConnection(connString);
  12. //myconn.Open();
  13. MySqlCommand mycommand = new MySqlCommand(query, myconn);
  14. MySqlDataAdapter myAdapter = new MySqlDataAdapter();
  15. myAdapter.SelectCommand = mycommand;
  16. DataTable dTable = new DataTable();
  17. myAdapter.Fill(dTable);
  18. dataGridView1.DataSource = dTable;
  19. //myconn.Close();
  20. }
  21. catch (MySql.Data.MySqlClient.MySqlException ex)
  22. {
  23. MessageBox.Show(ex.Message);
  24. }
  25.  
  26.  
  27. // this is my button
  28. private void btnShowInfo_Click(object sender, EventArgs e)
  29. {
  30. string query = "select * from students";
  31. fill_grid(query);
  32. }
  33.  
  34. What am i doing wrong. Thanks!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement