Public Sub SaveToDatabase()
' Set up a new blank data row if the user chose to Insert.
'---------------------------------------------------------
If booleanAddNew Then
objDataRowSingleParent = objDataSetParentDetails.Tables("Parent Details").NewRow()
End If
' Start the editing in the datarow.
'----------------------------------
objDataRowSingleParent.BeginEdit()
' Fill the data row with values from TextBoxes in the details group.
'-------------------------------------------------------------------
For Each objSingleControl In GroupBoxParentDetailsInfo.Controls
If TypeOf objSingleControl Is EditBox Then
' Each text box control starts with "EditBox" followed by the column name.
' This line will get the column name from the text box so data can be taken
' from the data row.
'--------------------------------------------------------------------------
strName = Mid(objSingleControl.Name, 8)
Try
' Grab a textbox's data and place it into a DataRow column.
'----------------------------------------------------------
objDataRowSingleParent(strName) = objSingleControl.text
Catch exException As Exception
MessageBox.Show("Data row column not the same as control name: " & _
objSingleControl.Name)
End Try
End If
Next
' Fill the data row with values from Payment Plan RadioButtons.
'--------------------------------------------------------------
If RadioButtonPlanA.Checked = True Then
objDataRowSingleParent("PaymentPlan") = "A"
End If
If RadioButtonPlanB.Checked = True Then
objDataRowSingleParent("PaymentPlan") = "B"
End If
If RadioButtonPlanC.Checked = True Then
objDataRowSingleParent("PaymentPlan") = "C"
End If
If RadioButtonPlanD.Checked = True Then
objDataRowSingleParent("PaymentPlan") = "D"
End If
' Finish the editing of the data row.
'------------------------------------
objDataRowSingleParent.EndEdit()
Try
' Add a new blank row into the Data Set if the user chose to Insert.
'-------------------------------------------------------------------
If booleanAddNew Then
objDataSetParentDetails.Tables("Parent Details").Rows.Add(objDataRowSingleParent)
End If
' Create an instance of the command builder.
'-------------------------------------------
objCommandBuilderParentDetails = New SqlCommandBuilder(objDataAdapterParentDetails)
If booleanAddNew Then
' Have the command builder create an Insert SQL command.
'-------------------------------------------------------
objDataAdapterParentDetails.InsertCommand = objCommandBuilderParentDetails.GetInsertCommand
Else
' Have the command builder create an update SQL command.
'-------------------------------------------------------
objDataAdapterParentDetails.UpdateCommand = objCommandBuilderParentDetails.GetUpdateCommand
End If
' Perform the update SQL command; then close the connection.
'-----------------------------------------------------------
objDataAdapterParentDetails.Update(objDataSetParentDetails, "Parent Details")
objDataSetParentDetails.Tables("Parent Details").AcceptChanges()
If booleanAddNew Then
' Re-load the Datset View because there is a new row of data.
'------------------------------------------------------------
PopulateDataGridWithParentNames()
' Close the connection opened by the insert command.
'---------------------------------------------------
objDataAdapterParentDetails.InsertCommand.Connection.Close()
Else
' Close the connection opened by the update command.
'---------------------------------------------------
objDataAdapterParentDetails.UpdateCommand.Connection.Close()
End If
' Reset this since it's not needed now.
'--------------------------------------
booleanAddNew = False
Catch saveException As Exception
Throw saveException ' to whoever called this sub routine.
End Try
End Sub