Results 1 to 10 of 10

Thread: add new records to database

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471

    add new records to database

    I have a datagrid filled with dataset. Now I add several new records from the grid and want to save them by clicking a "Save" button. How can I know which record is new, and how to save several new records at same time, in an other words, how to nevigate the grid? Thanks

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Check out this thread for the answer to your question,
    http://www.vbforums.com/showthread.p...hreadid=291167

  3. #3
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784
    Private Sub Save()
    SQLConnection1.Open()

    Dim UpdatedRows As System.Data.DataSet
    Dim InsertedRows As System.Data.DataSet
    Dim DeletedRows As System.Data.DataSet

    'these three are Data Tables that hold any changes that have been made to the dataset
    'since the last update.

    UpdatedRows = Me.Dataset1.GetChanges(DataRowState.Modified)
    InsertedRows = Me.Dataset1.GetChanges(DataRowState.Added)
    DeletedRows = Me.Dataset1.GetChanges(DataRowState.Deleted)

    Try
    'For each of these, we have to make sure that the Data Tables contain
    'any records, otherwise, we will get an error.

    If Not UpdatedRows Is Nothing Then
    Me.SQLDataAdapter1.Update(UpdatedRows)
    If Not InsertedRows Is Nothing Then
    Me.SQLDataAdapter1.Update(InsertedRows)
    If Not DeletedRows Is Nothing Then
    Me.SQLDataAdapter1.Update(DeletedRows)
    Me.Dataset1.AcceptChanges()
    Catch eUpdate As System.Exception
    Message("Err !!!, " & eUpdate.Message)
    End Try
    SQLConnection1.Close()

    End Sub

    Good luck

    Winanjaya

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Dim UpdatedRows As System.Data.DataSet
    Dim InsertedRows As System.Data.DataSet
    Dim DeletedRows As System.Data.DataSet

    'these three are Data Tables that hold any changes that have been made to the dataset
    'since the last update.
    I'm sorry, but DataSets and DataTables are not anywhere near the same thing. A "DataSet" is a container for one or more DataTables, DataRelations and other assorted objects.

    As for even considering the concept of mutiple DataTables for managing your inserts, updates and deletes, where on earth would you get the idea for this

    If you have a table that contains data from a datasource, the correct methodology for managing data in that table and returning the results to the datasource are by associating the correct command objects with your DataSet.

    VB Code:
    1. Dim cnLocal As SqlConnection = New SqlConnection("")
    2.         Dim dtLocal As DataTable
    3.         Dim daLocal As SqlDataAdapter = New SqlDataAdapter
    4.         Dim cmdSelect As SqlCommand = New SqlCommand("SELECT CategoryID, CategoryName, Description, Picture FROM Categories", cnLocal)
    5.         Dim cmdInsert As SqlCommand = New SqlCommand("INSERT INTO Categories(CategoryName, Description, Picture) VALUES (@CategoryName, @Description, @Picture); SELECT CategoryID, CategoryName, Description, Picture FROM Categories WHERE (CategoryID = @@IDENTITY", cnLocal)
    6.         cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"))
    7.         cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"))
    8.         cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Picture", System.Data.SqlDbType.VarBinary, 2147483647, "Picture"))
    9.         Dim cmdUpdate As SqlCommand = New SqlCommand("UPDATE Categories SET CategoryName = @CategoryName, Description = @Description, Picture = @Picture WHERE (CategoryID = @Original_CategoryID) AND (CategoryName = @Original_CategoryName); SELECT CategoryID, CategoryName, Description, Picture FROM Categories WHERE (CategoryID = @CategoryID)", cnLocal)
    10.         cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"))
    11.         cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"))
    12.         cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Picture", System.Data.SqlDbType.VarBinary, 2147483647, "Picture"))
    13.         cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, 0, 0, "CategoryID", System.Data.DataRowVersion.Original, Nothing))
    14.         cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, False, 0, 0, "CategoryName", System.Data.DataRowVersion.Original, Nothing))
    15.         cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CategoryID", System.Data.SqlDbType.Int, 4, "CategoryID"))
    16.         Dim cmdDelete As SqlCommand = New SqlCommand("DELETE FROM Categories WHERE (CategoryID = @Original_CategoryID) AND (CategoryNam = @Original_CategoryName)", cnLocal)
    17.         cmdDelete.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, 0, 0, "CategoryID", System.Data.DataRowVersion.Original, Nothing))
    18.         cmdDelete.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, False, 0, 0, "CategoryName", System.Data.DataRowVersion.Original, Nothing))
    19.         daLocal.SelectCommand = cmdSelect
    20.         daLocal.InsertCommand = cmdInsert
    21.         daLocal.UpdateCommand = cmdUpdate
    22.         daLocal.DeleteCommand = cmdDelete
    23.         daLocal.Fill(dtLocal)

    After you have made changes such as Inserts Updates or Deletes to your datatable, call the daLocal.Update method to return your changes to the database.

    This is the methodolgy which Microsoft teaches you to manage data with ADO.NET

    My suggestion on the code in the previous post is to learn ADO before you post something that could be much more harmful than helpful...

    Regards
    Last edited by CyberHawke; May 28th, 2004 at 01:23 PM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471
    thank you, CyberHawke.

    Actually i shoud thank that previous poster too, sometimes correcting mistake can help us understand things better.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    I must be the laziest programmer about, I don't even bother with any of the above, I simple use:

    Dim OledbStr As String = "Select * from FixturesandFittings where (Proprty = " & CStr(FindResult.Item) & ") and Unit = " & CStr(FindResult.Unit)
    Dim cmd6 As OleDbCommand = New OleDbCommand(OledbStr, Cn6)
    da6.SelectCommand = cmd6
    Cn6.Open()
    ' Fill the Dataset with selected Fixtures and Fittings values
    ds6.Clear()
    da6.Fill(ds6, "FixturesandFittings")
    Cn6.Close()

    that data is then bound to the datagrid as in:

    DataGrid2.SetDataBinding(ds6, "FixturesandFittings")

    then when they hit the save button:

    Dim Cb6 As OleDbCommandBuilder = New OleDbCommandBuilder(da6)


    da6.Update(ds6, "FixturesandFittings")



    Note:

    da6 is a predefined datadaptor
    ds6 a predefined dataset

    cn6 a predfined connection, which gets opened and closed appropiately


    amd yes this is working code

  7. #7
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: add new records to database

    Hawke,

    If you happen to read this thread again, can you please post your C# code that can insert / edit / delete records into datagrid. I have 3 buttons to add, save and delete records.
    there r no alternatives 4 hardwork.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: add new records to database


  9. #9
    Lively Member skv_noida's Avatar
    Join Date
    May 2005
    Location
    Noida, India
    Posts
    76

    Re: add new records to database

    Winanjaya is on right way!!!!!!!




  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: add new records to database

    Quote Originally Posted by RichardAtherton
    I must be the laziest programmer about, I don't even bother with any of the above, I simple use:

    Dim OledbStr As String = "Select * from FixturesandFittings where (Proprty = " & CStr(FindResult.Item) & ") and Unit = " & CStr(FindResult.Unit)
    Dim cmd6 As OleDbCommand = New OleDbCommand(OledbStr, Cn6)
    da6.SelectCommand = cmd6
    Cn6.Open()
    ' Fill the Dataset with selected Fixtures and Fittings values
    ds6.Clear()
    da6.Fill(ds6, "FixturesandFittings")
    Cn6.Close()

    that data is then bound to the datagrid as in:

    DataGrid2.SetDataBinding(ds6, "FixturesandFittings")

    then when they hit the save button:

    Dim Cb6 As OleDbCommandBuilder = New OleDbCommandBuilder(da6)


    da6.Update(ds6, "FixturesandFittings")



    Note:

    da6 is a predefined datadaptor
    ds6 a predefined dataset

    cn6 a predfined connection, which gets opened and closed appropiately


    amd yes this is working code
    CommandBuilders have three major flaws: They cannot be used where the result set is the result of a join. As far as I've been able to discern, there is no way to wrap the commands the build into a transaction. They will attempt to update all fields included in the corresponding select statement. They are fine for the simple case where these three factors are not an issue. I do recall reading, however, that Microsoft do not recommend CommandBuilders. Not that we all must do what Microsoft says, of course. I think that a lot of people are unaware of the fact that a DataAdapter can generate your delete, insert and update commands for you as actual, editable objects, complete with parameters and all the trimmings without "resorting" to CommandBuilders. When you add a DataAdapter to a form the wizard can create your extra commands for you. This wizard can also be invoked using the "Configure Data Adapter..." function at the bottom of the Properties window.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width