You are on page 1of 1

if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.

Text) ||
comboBox1.Text == "")
{
MessageBox.Show("Please Input all the fields");
}
else
{
connection.Open();
string selectquery = "SELECT * FROM student WHERE Name='" +
textBox1.Text + "';";
MySqlCommand command = new MySqlCommand(selectquery, connection);
reader = command.ExecuteReader();
if (reader.Read())
{
MessageBox.Show("Student existed.");
}
else
{
MySqlConnection connection2 = new
MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=ba
ntigue");
string iquery = "INSERT INTO student(ID,Name,Section,Gender)
VALUES(NULL,'" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text +
"');";
MySqlCommand command2 = new MySqlCommand(iquery, connection2);
connection2.Open();
MySqlDataReader reader2 = command2.ExecuteReader();
connection2.Close();
MessageBox.Show("Student Added.");
textBox1.Clear();
textBox2.Clear();
comboBox1.Text = "";
}
connection.Close();
}

You might also like