Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
235
Hierarchical Data Grid entry
posted

Hello,

I am attempting to create an UltraGrid which uses a dataset containing two datatables (with a datarelation linking them). I am using two dataadapters (one per table) to update the respective tables in my SQL Database. This works very well, so long as I don't create and child record at the same time. When attempting to do this, the Child record's field for the foreign key (AccountsContactGUID) is not getting filled in, and I get an error because it cannot have a null value. 

Here is the code I am using to bind and attempt to update this grid:

Imports System.Data.SqlClient

Public Class Form1
Public mDA1 As SqlDataAdapter
Public mDA2 As SqlDataAdapter
Dim mDS As DataSet

Public Sub New()

' This call is required by the designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
gSysOpt = New PD.Init.SysOpt(PD.GenCode.PDGen.GetApplicationPath.ToUpper)
gPDData = gSysOpt.PDData
End Sub

Private Sub FillGrid()
Dim strSQL As String
Dim cnLocal As IDbConnection
grdContact.Enabled = True

mDA1 = New SqlDataAdapter("SELECT * FROM AccountsContact WHERE Account = '3ST'", CType(gPDData.CN, SqlConnection))
Dim dataCommandBuilder1 As New SqlCommandBuilder(mDA1)
mDA1.InsertCommand = dataCommandBuilder1.GetInsertCommand
mDA1.UpdateCommand = dataCommandBuilder1.GetUpdateCommand
mDA1.DeleteCommand = dataCommandBuilder1.GetDeleteCommand
mDA2 = New SqlDataAdapter("SELECT * FROM AccountsContactRoles WHERE EXISTS (SELECT AccountsContactGUID From AccountsContact WHERE Account = '3ST')", CType(gPDData.CN, SqlConnection))
Dim dataCommandBuilder2 As New SqlCommandBuilder(mDA2)
mDA2.InsertCommand = dataCommandBuilder2.GetInsertCommand
mDA2.UpdateCommand = dataCommandBuilder2.GetUpdateCommand
mDA2.DeleteCommand = dataCommandBuilder2.GetDeleteCommand

'Dim DT1 As DataTable = gPDData.ExecuteDataTable(gPDData.CN, CommandType.Text, "SELECT * FROM AccountsContact WHERE Account = '3ST'", "AccountsContact")
'Dim DT2 As DataTable = gPDData.ExecuteDataTable(gPDData.CN, CommandType.Text, "SELECT * FROM AccountsContactRoles", "AccountsContactRoles")
mDS = New DataSet
mDA1.Fill(mDS, "AccountsContact")
mDA2.Fill(mDS, "AccountsContactRoles")
Dim relation As DataRelation = mDS.Relations.Add("AccountsContactGUID", mDS.Tables("AccountsContact").Columns("AccountsContactGUID"), mDS.Tables("AccountsContactRoles").Columns("AccountsContactGUID"))

'Dim dr As New DataRelation("DR", DT1.Columns("AccountsContactGUID"), DT2.Columns("AccountsCOntactGUID"), True)
'mDS.Relations.Add(relation)
grdContact.DataSource = mDS
Debug.Print("POTATO")
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
FillGrid()
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
grdContact.UpdateData()
mDA1.Update(mDS.Tables("AccountsContact"))
mDA2.Update(mDS.Tables("AccountsContactRoles"))
End Sub

Private Sub grdContact_AfterRowInsert(sender As Object, e As Infragistics.Win.UltraWinGrid.RowEventArgs) Handles grdContact.AfterRowInsert
If e.Row.ParentRow IsNot Nothing Then
e.Row.Cells("AccountsContactGUID").Value = e.Row.ParentRow.Cells("AccountsContactGUID").Value
End If
End Sub
End Class

Parents
No Data
Reply
  • 21795
    Offline posted

    Hello Nick,

    Thank you for posting in our forum.

    I have tried the code you send us with a local database I have created (as I do not have access to your data base as well as to the PD class). My database has only to tables Test and TestChild, please look at the attached diagram. I have tried to add some rows to the child and parent table with success.

    So please give me some more info about your scenario. When do you receive this error? Please give me a step by step instructions, so I can reproduce this at my site. Can you send me the stack trace of this error?

    Looking forward to your reply.

    Thank you for using Infragistics Components.

Children