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 SubEnd Class
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.
Thank you for your Response Milko, I have attached a script to create the tables I am working with.
The PD class here is just a helper class to create the database connection.
To re-create my issue, I first add a new parent row (In my tables this requires both an Account and a Contact Name).
From there, I add a child table row. The child table row should be inheriting the GUID foreign key via the relationship, but does not appear to be doing so.
Next, I click the save button. The error occurs on mDA2.Update(mDS.Tables("AccountsContactRoles"))
Here is the stack trace:
"System.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'AccountsContactGUID', table 'JDWITran6.dbo.AccountsContactRoles'; column does not allow nulls. INSERT fails. The statement has been terminated. at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.Update(DataTable dataTable) at PDHierarchyGridTest.Form1.btnSave_Click(Object sender, EventArgs e) in C:\Users\nickd\SkyDrive\CodeProjects\PDHierarchyGridTest\PDHierarchyGridTest\Form1.vb:line 60 ClientConnectionId:03a6e522-e885-485b-a893-2f56cc83855b Error Number:515,State:2,Class:16"
What I think would be a potential solution would be to generate the AccountsContactGUID for the Parent Row upon adding a row to the grid, and assigning it to that cell. Then passing that GUID to the Child row when creating it. The problem I have there is that the cell in the Parent row for AccountsContactGUID doesn't appear to be able to be assigned a value due to the Datarelation (When I try and access it's value, it returns {System.Data.RelatedView}
Many thanks for your assistance,
Nick
Hi,
We are still following this forum thread.
Please feel free to let us know if you still have any questions on this matter.