Unable to add record?

pryspr01

Member
Joined
Jan 5, 2006
Messages
15
Programming Experience
Beginner
Not sure what it's doing, but its not adding any records, looks to be doing nothing. I know im missing the right variable there where the ????'s are, what's that supposed to be? If I need to add soemthing else please let me know.

VB.NET:
    Private Sub MenuItem3_Click_1(ByVal txtLNAME As String, ByVal txtFNAME As String)

        Dim newRow As DataRow = DataSet12.Tables("ProspectTable").NewRow()

        newRow("LastName") = txtLNAME
        newRow("FirstName") = txtFNAME


        DataSet12.Tables("ProspectTable").Rows.Add(newRow)

        OleDbConnection.Open()

        ????.Update(DataSet12, "ProspectTable")


        OleDbConnection.Close()

    End Sub
 
The Update command is issued to a data adapter.
OleDbDataAdapter1?

Assuming you have a valid update statement: (the data adapter wizard can generate the statements for you) you can update with the 2 subs below.

VB.NET:
   Public Sub UpdateDataSet()
        Dim datasetChanges As DataSet1 = New DataSet1
        Me.BindingContext(DataSet12, "ProspectTable").EndCurrentEdit()
        datasetChanges = CType(DataSet12.GetChanges, DataSet1)
        If (Not (datasetChanges) Is Nothing) Then
            Try
                Me.UpdateDataSource(datasetChanges)
                DataSet12.AcceptChanges()
            Catch eUpdate As System.Exception
                Throw eUpdate
            End Try
        End If
    End Sub

    Public Sub UpdateDataSource(ByVal ChangedRows As DataSet1)
        Try
            If (Not (ChangedRows) Is Nothing) Then
                Me.OleDbConnection1.Open()
                OleDbDataAdapter1.Update(ChangedRows)
            End If
        Catch updateException As System.Exception
            Throw updateException
        Finally
            Me.OleDbConnection1.Close()
        End Try
    End Sub
Then call the update like this whenever you need to:
VB.NET:
        Try
            Me.UpdateDataSet()
        Catch eLoad As System.Exception
            System.Windows.Forms.MessageBox.Show(eLoad.Message)
        End Try

PS. This is the type of code a dataform wizard would generate for you on an update button.
 
well the dataform wizard was helpful, however I made tiny changes to it, and it will not load the dataset for some reason. It all looks fine to me, datasetname = Dataset1 design name = objDataset1. On load click it simply doesnt load it. says record 0 of 0. Know why?
VB.NET:
    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Try
            'Attempt to load the dataset.
            Me.LoadDataSet()
        Catch eLoad As System.Exception
            'Add your error handling code here.
            'Display error message, if any.
            System.Windows.Forms.MessageBox.Show(eLoad.Message)
        End Try
        Me.objDataset1_PositionChanged()

    End Sub

VB.NET:
     Public Sub LoadDataSet()
        'Create a new dataset to hold the records returned from the call to FillDataSet.
        'A temporary dataset is used because filling the existing dataset would
        'require the databindings to be rebound.
        Dim objDataSetTemp As Perabus.Dataset1
        objDataSetTemp = New Perabus.Dataset1
        Try
            'Attempt to fill the temporary dataset.
            objDataSetTemp = CType(Me.objDataset1.Clone, Perabus.Dataset1)
        Catch eFillDataSet As System.Exception
            'Add your error handling code here.
            Throw eFillDataSet
        End Try
        Try
            'Empty the old records from the dataset.
            objDataset1.Clear()
            'Merge the records into the main dataset.
            objDataset1.Merge(objDataSetTemp)
        Catch eLoadMerge As System.Exception
            'Add your error handling code here.
            Throw eLoadMerge
        End Try

    End Sub
 
VB.NET:
        Try
            '[COLOR=Green]Attempt to fill the temporary dataset [COLOR=Red]with an empty dataset[/COLOR].[/COLOR]
            objDataSetTemp = CType(Me.objDataset1.Clone, Perabus.Dataset1)
        Catch eFillDataSet As System.Exception
           [COLOR=Green] 'Add your error handling code here.[/COLOR]
            Throw eFillDataSet
        End Try
Trying to fill a temp dataset setup by this statement,
objDataSetTemp = New Perabus.Dataset1
with Perabus.Dataset1 that hasn't been filled yet?
What are you trying to do here?
Where do you open your connection and fill the data adapter?
You should have an empty dataset, you haven't went out and got any data yet.

VB.NET:
objDataset1.Clear()
[COLOR=Green] 'Merge the [COLOR=Red]non existing[/COLOR] records into the main dataset.[/COLOR]
objDataset1.Merge(objDataSetTemp)
 
This was from the data form wizard in VB.NET. I tought it was supposed to use the exisiting dataset and fill a temporary dataset with it, then adding more data to the temporary dataset, and then it looks for changes in the origonal one and commis the changes. However the change you gave still had no affect. Not sure why.
 
I'm sorry, I was just kidding, the changes I made were to the smart alick comments - just a joke, appearently not funny. The dataform wizard should have called:

VB.NET:
         Try
            Me.FillDataSet(objDataSetTemp)
        Catch eFillDataSet As System.Exception
            Throw eFillDataSet
        End Try
the data gets loaded here:

VB.NET:
    Public Sub FillDataSet(ByVal dataSet As Perabus.Dataset1)
        dataSet.EnforceConstraints = False
        Try
            Me.OleDbConnection1.Open()
            Me.OleDbDataAdapter1.Fill(dataSet)
        Catch fillException As System.Exception
            Throw fillException
        Finally
            dataSet.EnforceConstraints = True
            Me.OleDbConnection1.Close()
        End Try

    End Sub
 
Hah yea sorry after hours working on this and being new to VB.NET, your comments were lost in translation hah. I have been working more with the dataform wizard which does give me a nice clean layout to start with so I thank you for giving me that code and helping me clean that up.
 
Back
Top