Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Updating DataSource

Updating DataSource

Scheduled Pinned Locked Moved C#
questionhelpannouncement
8 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mazdak
    wrote on last edited by
    #1

    I have a dataset,and dataadapter.I add a row to dataset like this:

    DataRow row = dataSet11.Tables\["Table2"\].NewRow();
    row\["Username"\] = str1;
    row\["Password"\] = str2;
    row\["ID"\] = 65;
    
    dataSet11.Tables\["Table2"\].Rows.Add(row);
    

    Now I want to make this change to datasource,so I used this: oleDbDataAdapter1.Update(dataSet11,"Table2"); But unhandled error thrown for this line,and it said no valid update command. I looked into some MSDN doc and there were talking about modifying recordsets in dataset not add new records,now how can I reflect changes and additions from dataset to datasource?Does Update() only for midifying or its for adding records too? Thanks Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
    Wish You Were Here-Pink Floyd-1975

    A 1 Reply Last reply
    0
    • M Mazdak

      I have a dataset,and dataadapter.I add a row to dataset like this:

      DataRow row = dataSet11.Tables\["Table2"\].NewRow();
      row\["Username"\] = str1;
      row\["Password"\] = str2;
      row\["ID"\] = 65;
      
      dataSet11.Tables\["Table2"\].Rows.Add(row);
      

      Now I want to make this change to datasource,so I used this: oleDbDataAdapter1.Update(dataSet11,"Table2"); But unhandled error thrown for this line,and it said no valid update command. I looked into some MSDN doc and there were talking about modifying recordsets in dataset not add new records,now how can I reflect changes and additions from dataset to datasource?Does Update() only for midifying or its for adding records too? Thanks Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
      Wish You Were Here-Pink Floyd-1975

      A Offline
      A Offline
      Andres Manggini
      wrote on last edited by
      #2

      If you're adding a new Row and then calling Update on the DataAdapter, you should assign something to the InsertCommand Property of DataAdapter. InsertCommand takes a SQL Sentence (string) or OleDbCommand. Or the alternative using OleDbCommandBuilder. This class generates the SQL Statements. See: InsertCommand DeleteCommand SelectCommand OleDbCommand OleDbCommandBuilder Andres Manggini. Buenos Aires - Argentina.

      M 1 Reply Last reply
      0
      • A Andres Manggini

        If you're adding a new Row and then calling Update on the DataAdapter, you should assign something to the InsertCommand Property of DataAdapter. InsertCommand takes a SQL Sentence (string) or OleDbCommand. Or the alternative using OleDbCommandBuilder. This class generates the SQL Statements. See: InsertCommand DeleteCommand SelectCommand OleDbCommand OleDbCommandBuilder Andres Manggini. Buenos Aires - Argentina.

        M Offline
        M Offline
        Mazdak
        wrote on last edited by
        #3

        Thanks Andres,I got the point, Now if I want to add 10 Records to DataSet and want to update to DataAdapter,Should I use 10 InsertCommand ,Or is there anyway to put all of them in one InsertCommand? Thanks Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
        Wish You Were Here-Pink Floyd-1975

        A 1 Reply Last reply
        0
        • M Mazdak

          Thanks Andres,I got the point, Now if I want to add 10 Records to DataSet and want to update to DataAdapter,Should I use 10 InsertCommand ,Or is there anyway to put all of them in one InsertCommand? Thanks Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
          Wish You Were Here-Pink Floyd-1975

          A Offline
          A Offline
          Andres Manggini
          wrote on last edited by
          #4

          The same InsertCommand will be call as many times as records were added. That's the beauty of it, you can insert a few records, delete others and make a couple of changes on others, then set the three commands (InsertCommand, DeleteCommand, UpdateCommand) and let the DataAdapter do all the updates to the Database. Andres Manggini. Buenos Aires - Argentina.

          M 1 Reply Last reply
          0
          • A Andres Manggini

            The same InsertCommand will be call as many times as records were added. That's the beauty of it, you can insert a few records, delete others and make a couple of changes on others, then set the three commands (InsertCommand, DeleteCommand, UpdateCommand) and let the DataAdapter do all the updates to the Database. Andres Manggini. Buenos Aires - Argentina.

            M Offline
            M Offline
            Mazdak
            wrote on last edited by
            #5

            hmmm,I got it.But there is another question for me here: Each time I make a change to dataset,I have to update DataAdapter,so for large database I don't think its good. Or I can update DataAdapter after all changes.What is a solution for that too? Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
            Wish You Were Here-Pink Floyd-1975

            A 1 Reply Last reply
            0
            • M Mazdak

              hmmm,I got it.But there is another question for me here: Each time I make a change to dataset,I have to update DataAdapter,so for large database I don't think its good. Or I can update DataAdapter after all changes.What is a solution for that too? Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
              Wish You Were Here-Pink Floyd-1975

              A Offline
              A Offline
              Andres Manggini
              wrote on last edited by
              #6

              The idea is you make all the changes needed on the DataSet, once you're done and want to commit these changes to the database you call Update on the DataAdapter, and the changes will be reflected on the Database. There is no need (of course this is depending on your design) to save the dataset after each change, it works sort of a batch process. Hope that clarify a bit. If you have doubts just ask again :) Andres Manggini. Buenos Aires - Argentina.

              A 1 Reply Last reply
              0
              • A Andres Manggini

                The idea is you make all the changes needed on the DataSet, once you're done and want to commit these changes to the database you call Update on the DataAdapter, and the changes will be reflected on the Database. There is no need (of course this is depending on your design) to save the dataset after each change, it works sort of a batch process. Hope that clarify a bit. If you have doubts just ask again :) Andres Manggini. Buenos Aires - Argentina.

                A Offline
                A Offline
                Andres Manggini
                wrote on last edited by
                #7

                One more thing.. If you're dealing with large DataSet's, there is this method: GetChanges. Doc: Gets a copy of the DataSet containing all changes made to it since it was last loaded It can be very useful in distributed scenarios. Andres Manggini. Buenos Aires - Argentina.

                M 1 Reply Last reply
                0
                • A Andres Manggini

                  One more thing.. If you're dealing with large DataSet's, there is this method: GetChanges. Doc: Gets a copy of the DataSet containing all changes made to it since it was last loaded It can be very useful in distributed scenarios. Andres Manggini. Buenos Aires - Argentina.

                  M Offline
                  M Offline
                  Mazdak
                  wrote on last edited by
                  #8

                  Thanks Andres,Now I have to write some codes to understand all this things:) Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
                  Wish You Were Here-Pink Floyd-1975

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups