Hi Guys need a bit of help!
Basically I have a parent table called users which stores all the user info with a prime key field called ID. Now in the child table which has a seperate foreign key
associated with the ID field in the parent table.
What I would like to do is be able to populate the foreign key once once the user uploads data into the child table with the user ID it has.
So far I have created the associated foreign key with ID field in the user table and also programmly through data-relation related the table but now sure on how to get the value from the relation.
The foreign key in the child table is called 'U_ID' as shown in the snipet velow.
Please help :D
Dim cs As New SqlConnection("Data Source=PC\SQLEXPRESS;Initial Catalog=ForumCrawl;Integrated Security=True")
Dim da As New SqlDataAdapter("SELECT * FROM search_result", cs)
Dim da2 As New SqlDataAdapter("SELECT * FROM users", cs)
Dim ds As New DataSet
Dim dsNewRow As DataRow
Dim cmb As SqlCommandBuilder = New SqlCommandBuilder(da)
cmb.GetUpdateCommand()
Dim dateToDisplay As DateTime = DateTime.Now
da2.Fill(ds, "users")
da.Fill(ds, "search_result")
Dim relation As DataRelation
Dim table1column As DataColumn
Dim table2column As DataColumn
'retrieve columns
table1column = ds.Tables("users").Columns("ID")
table2column = ds.Tables("search_result").Columns("U_ID")
relation = New DataRelation("relation", table1column, table2column)
ds.Relations.Add(relation)
MsgBox("Data relation completed")
dsNewRow = ds.Tables("search_result").NewRow
dsNewRow.Item("URL") = tb_URL.Text
dsNewRow.Item("page_content") = tb_Parse.Text
dsNewRow.Item("U_ID") =
dsNewRow.Item("date_created") = dateToDisplay.ToString
ds.Tables("search_result").Rows.Add(dsNewRow)
da.UpdateCommand = cmb.GetUpdateCommand
Try
da.Update(ds, "search_result")
MessageBox.Show("New record added to the database!", _
"Success", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button1)
Catch ex As Exception
MessageBox.Show("Data already exists in database! Please check data to avoid duplicates", _
"Database Error!", MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
End Try
cs.Close()