Angie S

Angie S

  • NA
  • 1
  • 0

Checking that values do not exist in the database before inserting them...

Apr 6 2007 7:39 AM
Hello,

I am new to visual studio and i am using the 2005 one with asp .net 2.0. I have written code that inserts into the database but i have failed to write code that checks that the surname and forename are already in the database and therefore if they are, they should not be added again or should produce an error message that says that they exist. this is part of the code i have for inserting into the database and it works.. Can i please get help on how to search the database to make sure that the surname and forename do not exist or exist and if they do they should not be added and if they dont then they should be added... Please help.....

'strSQL = "insert into researcher (R_Surname) values ('" & TextBox9.Text & "')"

strSQL = "insert into researcher (R_ResearcherID, R_Surname, R_Forename, R_Initials, "

strSQL = strSQL + "R_Title, R_YOB, R_Camp, R_Mode, R_Position, R_Category, R_Phone, "

strSQL = strSQL + "R_Email, R_Web, R_Start, R_End, R_RAECombi, R_Qualifications, "

strSQL = strSQL + "R_Honours, R_ResearchInterests, R_Fax, SRIS, DataProtectionOptIn, "

strSQL = strSQL + "D_DepartmentID, R_MemoExp) values ('" + (TextBox1.Text) + "', '" + (TextBox9.Text) + "',"

strSQL = strSQL + "'" + (TextBox7.Text) + "', '" + (TextBox5.Text) + "', '" + (TextBox3.Text) + "',"

strSQL = strSQL + "'" + (TextBox6.Text) + "', '" + (DropDownList3.SelectedValue) + "', '" + (TextBox10.Text) + "',"

strSQL = strSQL + "'" + (DropDownList1.SelectedValue) + "', '" + (DropDownList2.SelectedValue) + "', '" + (TextBox13.Text) + "',"

strSQL = strSQL + "'" + (TextBox15.Text) + "', '" + (TextBox16.Text) + "', '" + (TextBox12.Text) + "', "

strSQL = strSQL + "'" + (TextBox11.Text) + "', '" + (TextBox19.Text) + "', '" + (TextBox17.Text) + "', "

strSQL = strSQL + "'" + (TextBox18.Text) + "','" + (TextBox4.Text) + "', '" + (TextBox14.Text) + "', "

strSQL = strSQL + "'" + CheckBox2.Text + "','" + CheckBox1.Text + "', '" + (DropDownList4.SelectedValue) + "', '" + (TextBox2.Text) + "')"

Dim myCommand As New SqlClient.SqlCommand(strSQL, oleDbConnection)

oleDbConnection.Open()

myCommand.ExecuteReader(CommandBehavior.CloseConnection)

oleDbConnection.Close()