As the title says my datagrid was not showing after I click the save button
here's my classes:
class myLogin
{
static MySqlConnection myConn = new MySqlConnection("datasource=localhost; database = mydbs; username=root; password=;");
static MySqlDataAdapter adapter;
static DataTable table = new DataTable();
static MySqlCommand cmd;
public static DataSet exec(
String que)
{
DataSet ds = new DataSet();
try
{
myConn.Open();
adapter = new MySqlDataAdapter(que,myConn);
adapter.Fill(ds);
}
catch (Exception) {
}
finally
{
myConn.Close();
}
return ds;
}
public static void excQrry(String sqlstr)
{
try
{
myConn.Open();
cmd = new MySqlCommand(sqlstr, myConn);
cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
myConn.Close();
}
}
the second class:
class Grads
{
public int grad_stud_id { get; set; }
public String grad_stud_name { get; set; }
public String grad_current_address { get; set; }
public String grad_place_of_birth { get; set; }
public String grad_date_of_birth { get; set; }
public String grad_citizenship { get; set; }
public Grads(String name, String address, String place, String date, String citizenship)
{
grad_stud_name = name;
grad_current_address = address;
grad_place_of_birth = place;
grad_date_of_birth = date;
grad_citizenship = citizenship;
}
public Grads() { }
public static DataSet fetch()
{
try
{
String que = "SELECT * FROM graduated_student";
return myLogin.exec(que);
}
catch (Exception)
{
return null;
}
}
public static DataSet fetch(String choice)
{
try
{
String que = "SELECT * FROM graduated_student WHERE " + choice;
return myLogin.exec(que);
}
catch(Exception)
{
return null;
}
}
public String save()
{
try
{
String qrry = String.Format("INSERT INTO graduated_student(grad_stud_name, grad_current_address, grad_place_of_birth, grad_date_of_birth, grad_citizenship) VALUES ('"+grad_stud_name+"','"+grad_current_address+"','"+grad_place_of_birth+"','"+grad_date_of_birth+"','"+grad_citizenship+"')");
myLogin.excQrry(qrry);
return "Alumnus " + grad_stud_name + " has been successfully save ";
}
catch(Exception e)
{
return "Something went wrong. \n" + e.Message.ToString();
}
}
}
This is the class where the addbtn is:
private void btnAdd_Click(object sender, EventArgs e)
{
if (btnAdd.Text.Equals("&Add"))
{
groupBox1.Enabled = true;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
btnDelete.Enabled = false;
btnSearch.Enabled = false;
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox1.Focus();
btnAdd.Text = "Save";
btnEdit.Text = "Cancel";
}
else
{
if (textBox1.Text.Equals(""))
{
MessageBox.Show("Please input the Name of Alumnus");
textBox1.Focus();
}
else if (textBox2.Text.Equals(""))
{
MessageBox.Show("Please input the Current Address");
textBox2.Focus();
}
else if (textBox3.Text.Equals(""))
{
MessageBox.Show("Please input Place of Birth ");
textBox3.Focus();
}
else if (textBox4.Text.Equals(""))
{
MessageBox.Show("Please input Date of Birth");
textBox4.Focus();
}
else if(textBox5.Text.Equals(""))
{
MessageBox.Show("Please input Citizenship");
textBox5.Focus();
}
else
{
Grads g = new Grads(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text);
String msg = g.save();
MessageBox.Show(msg, "Save Alumnus Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
textBox5.Enabled = false;
btnAdd.Text = "&Add";
btnEdit.Text = "&Edit";
btnDelete.Enabled = true;
btnSearch.Enabled = true;
ACP_Load(this, null);
}
}
}
private void ACP_Load(object sender, EventArgs e)
{
try {
groupBox1.Enabled = false;
dataGridView1.DataSource = Grads.fetch().Tables[0];
dataGridView1.Columns[0].Width = 75;
dataGridView1.Columns[0].HeaderText = "Student Id";
dataGridView1.Columns[1].Width = 75;
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[2].Width = 75;
dataGridView1.Columns[2].HeaderText = "Current Address";
dataGridView1.Columns[3].Width = 76;
dataGridView1.Columns[3].HeaderText = "Place of Birth";
dataGridView1.Columns[4].Width = 77;
dataGridView1.Columns[4].HeaderText = "Date of Birth";
dataGridView1.Columns[5].Width = 80;
dataGridView1.Columns[5].HeaderText = "Citizenship";
dataGridView1_CellContentClick(this, null);
dataGridView1.Refresh();
} catch (Exception) { }
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
textBox2.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
textBox3.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
}
Can someone tell me what is wrong?
God Bless