|
-
Apr 21st, 2022, 09:27 AM
#1
Thread Starter
Member
[RESOLVED] DataAdapter thinks everything is NULL
I'm at a complete loss and very confused. I have two different WinForms that perform almost exactly the same - that is to retrieve data from the database and be able to edit & save that data back, or to create new records. On form "A" everything works as expected with no errors. On form "B" it thinks controls are NULL if that column in the database is set to "Not NULL." Form "B" uses almost identical code to form "A" for building everything, as well as editing and saving records. If I change a "problem" column to accept nulls, then the error goes away for that column. This is not an issue on form "A" even though it has a similar database setup as well.
IF I do a CType(bs_Record.current, datarowview).Item("Room") I get the value expected.
If I do a dt_Record.Rows.Item(772).item("Room") I get the value expected.
Here is the error I am getting:

I am not understanding why the DataAdapter thinks the controls are NULL, even though the BindingSource AND DataTable both have the information in them.
This is the code to build the DA, DT, and BS:
Code:
Using SQLcmd As New SqlCommand With {
.Connection = Vars.sqlConn,
.CommandText = "SELECT * FROM [BAD].[data_Lab_Asset] ORDER BY [BGL_ID] DESC"}
da_Record.SelectCommand = SQLcmd
Dim sqlCB As New SqlCommandBuilder(da_Record)
AddHandler da_Record.RowUpdated, AddressOf OnRowUpdated
da_Record.Fill(dt_Record)
dt_Record.Columns("OP_Status_ID").DefaultValue = 3
'** ALL Check boxes need a Default Value for the "Add New" to work
dt_Record.Columns("Retired").DefaultValue = False
dt_Record.Columns("Storage").DefaultValue = False
dt_Record.Columns("Transferred").DefaultValue = False
dt_Record.Columns("Removed").DefaultValue = False
dt_Record.Columns("Calibration_Req").DefaultValue = False
dt_Record.Columns("TQC_Req").DefaultValue = False
dt_Record.Columns("TQC_Built").DefaultValue = False
dt_Record.Columns("TQC_Worklist_ID").DefaultValue = 0
bs_Record.DataSource = dt_Record
bsnav_Record.BindingSource = bs_Record
End Using
Here is a snippet of the Binding code:
Code:
Private Sub BindControls()
Try
txtRecID.DataBindings.Add("Text", bs_Record, "ID_Lab_Asset")
cboxSupportGroup.DataBindings.Add("SelectedValue", bs_Record, "Support_Group_ID")
cboxArea.DataBindings.Add("SelectedValue", bs_Record, "Work_Area_ID")
txtRoom.DataBindings.Add("Text", bs_Record, "Room")
cboxClass.DataBindings.Add("SelectedValue", bs_Record, "Class_ID")
cboxSubclass.DataBindings.Add("SelectedValue", bs_Record, "Subclass_ID")
cboxSubcategory.DataBindings.Add("SelectedValue", bs_Record, "Subcategory_ID")
chkCalReq.DataBindings.Add("Checked", bs_Record, "Calibration_Req")
dtpCalDue.DataBindings.Add("Text", bs_Record, "Calibration_Due", True, DataSourceUpdateMode.OnValidation, " ", "MM/dd/yyyy")
dtpPurch.DataBindings.Add("Text", bs_Record, "Date_Purchased", True, DataSourceUpdateMode.OnValidation, " ", "MM/dd/yyyy")
dtpRecvd.DataBindings.Add("Text", bs_Record, "Date_Received", True, DataSourceUpdateMode.OnValidation, " ", "MM/dd/yyyy")
txtRecvdFrom.DataBindings.Add("Text", bs_Record, "Received_From")
txtComments.DataBindings.Add("Text", bs_Record, "Comments")
Catch ex As Exception
CustExErrorMsg(Name, Reflection.MethodBase.GetCurrentMethod().Name, GetExceptionInfo(ex))
End Try
End Sub
Code to create new row in BindingSource:
Code:
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles BNavAddNewItem.Click
Try
bs_Record.AddNew()
bs_Related.Filter = "BIOMED_Tag = 'XXX'"
bs_Validation.Filter = "Lab_Asset_ID = 'XXX'"
dtpPurch.Value = New Date(Date.Now.Year, Date.Now.Month, Date.Now.Day - 1)
dtpRecvd.Value = New Date(Date.Now.Year, Date.Now.Month, Date.Now.Day - 1)
dtpInUse.Value = New Date(Date.Now.Year, Date.Now.Month, Date.Now.Day - 1)
EnableControls()
Catch ex As Exception
CustExErrorMsg(Name, Reflection.MethodBase.GetCurrentMethod().Name, GetExceptionInfo(ex))
End Try
End Sub
And finally the code to save the record:
Code:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles BNavSaveItem.Click
Try
If IsComplete() Then
If blnNew Then If IsDuplicate() Then GoTo TheEnd
Me.Validate()
bs_Record.EndEdit()
da_Record.Update(dt_Record)
If blnNew Then bs_Record.Position = bs_Record.Find("ID_Lab_Asset", ID_New)
TheEnd:
IsDirty = False
blnEdit = False
blnNew = False
DisableControls()
End If
Catch ex As Exception
CustExErrorMsg(Name, Reflection.MethodBase.GetCurrentMethod().Name, GetExceptionInfo(ex))
End Try
End Sub
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|