This is my code for the Register
button:
private void button1_Click(object sender, EventArgs e)
{
string gender = "Male";
if (radioButton1.Checked)
{
gender = "Male";
}
else if (radioButton2.Checked)
{
gender = "female";
}
SqlConnection con = new SqlConnection("Data Source=LAPTOP-78IBEHAB;Initial Catalog=student;Integrated Security=True");
con.Open();
SqlCommand cmd2 = new SqlCommand("select Regno from Register where Regno = @Regno",con);
cmd2.Parameters.AddWithValue("Regno",comboBox1.Text);
SqlDataReader sdr = cmd2.ExecuteReader();
if (sdr.Read())
{
con.Close();
MessageBox.Show("Duplicate Reg No");
}
else
{
con.Close();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Register(Firstname, Lastname, DOB, GENDER, ADDRESS, EMAIL, MOBILE, HOME, PARENT, NIC, CONTACT,COURSE)\r\nvalues\r\n('" + textBox1.Text + "', '" + textBox2.Text + "', '" + dateTimePicker1.Text + "', '" + gender + "', '" + textBox3.Text + "', '" + textBox4.Text + "', '" + textBox5.Text + "', '" + textBox6.Text + "', '" + textBox7.Text + "', '" + textBox8.Text + "', '" + textBox9.Text + "','"+comboBox2.Text+"')";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Registration successfull");
textclear();
comboBox1.Items.Clear();
comdata();
}
}
I just want to pop a message box when trying to insert irrelevant values to given textbox eg: name = 123456(int),phone = john(varchar) and NotNUll