Question Problem: Saving foreign key with records along with other updates to datagridview.

ineedhelpbigtime

New member
Joined
Nov 30, 2009
Messages
1
Programming Experience
1-3
I need to update a column in my database (access) which will not be input by the user, but is available as a variable in the form. My current code is as follows:

VB.NET:
Expand Collapse Copy
Public Class frmContacts
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim projectid As Integer

    Private Sub frmContacts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        projectid = 1 // set here manually but would be set using a passed variable.
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\EZaudit.mdb"
        con.Open()

        sql = "SELECT contacts.p_id, contacts.con_name as Name, contacts.con_position AS JobTitle, contacts.con_email AS Email, contacts.con_phone AS PhoneNo FROM project, contacts WHERE project.p_id = contacts.p_ID AND contacts.p_id=" & projectid & ""
        da = New OleDb.OleDbDataAdapter(sql, con)

        da.Fill(ds, "clientdetails")

        con.Close()

        dgContacts.DataSource = ds.Tables("clientdetails")

        ds.Tables("clientdetails").Rows(0).Item(0) = projectid

        dgContacts.Visible = True

    End Sub
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim cb As New OleDb.OleDbCommandBuilder(da)
        
        da.Update(CType(dgContacts.DataSource, DataTable))

        MsgBox("Updated.")


    End Sub

This code works, but for each row added, i need to update the customer.p_id field with the projectid variable.

I havent got a clue on this, i must have spent 5 hours trying to find the solution for what seems to me, should be, a fairly easy thing to do.

Any help is much appreciated.
 
Back
Top