Question : INSERT with MSSQL Foreign Keys

I'm having a problem inserting into MSSQL table.

Table1
MainID (Primary Key)

Table2
key
MainID (Foreign Key)
Field3

dbo.table1 mainID is linked to dbo.table2's mainID with a cascading delete. Table1 is already populated with data. I am trying to Insert a new record into table2 which is related to table1.

Here is the code I am trying. I am using VB to insert the data.
IF NOT EXISTS (SELECT * FROM table2 WHERE mainID= '5') INSERT INTO table2 (field3, mainID) VALUES ('TEST', '5')

The error I get is
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_table2_table1".

I am able to successfully add the record through Query Analzyer and just editing the table manually through Studio manager without errors.

Here is the actual VB code that I am trying to work with that is failing.

1:
2:
3:
4:
5:
6:
7:
8:
9:
Dim queryMainID As String = "IF NOT EXISTS (SELECT * FROM table2 WHERE mainID = '" & MainID & "') INSERT INTO table2 (field3, mainid) VALUES ('TEST', '5') SELECT SCOPE_IDENTITY() as mainID"
Dim dscmd As New SqlDataAdapter(UpdateSTS, DB)
Dim ds As New DataSet()
dscmd.Fill(ds, "MainIDQuery")
Dim dt As DataTable = ds.Tables.Item("MainIDQuery")
Dim rowJob As DataRow
For Each rowJob In dt.Rows
  Dim field3 = rowJob.Item("field3").ToString
Next

Answer : INSERT with MSSQL Foreign Keys

Hi Saratcm

Basically SQL Server 2008 does support Transparent Data Encryption (TDE) but you have to enable it at the Database level and not necessarily during the backup stage itself.

Please take a look at this link as it provides information about the same thing you would like to do, hopefully it will help.

http://www.mssqltips.com/tip.asp?tip=1514

Good Luck.
 
Random Solutions  
 
programming4us programming4us