I just want to insert some entries into a SQL Server database. But I need to check before inserting. The key point is that the table includes two columns id
and name
. The presented code should check both columns and prevent the entry to be inserted if the entry is exactly the same as in the SQL Server database.
Meaning that two columns of data table are same as two entries going to be inserted. I checked the forums but couldn't find any useful solution for my project.
private void button4_Click(object sender, EventArgs e)
{
try
{
objConnection.Open();
string query = "INSERT INTO TutorTable(Tid, Tname) VALUES(N'" + tidTextBox.Text + "','" + tnameTextBox.Text + "')";
SqlDataAdapter SDA = new SqlDataAdapter(query, objConnection);
SDA.SelectCommand.ExecuteNonQuery();
objConnection.Close();
MessageBox.Show("ok!");
}
catch(Exception ex)
{
MessageBox.Show("error");
}
objConnection.Close();
}