Results 1 to 2 of 2

Thread: How to modify a record from reader?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    How to modify a record from reader?

    I am new to C# and learning as I go So I hope you can help. I am reading from SQL using a reader.

    I than want to manipulate the data that was read and write to a new table

    I have the read and write piece working, now I am working on the syntax of changing the value in the reader.

    Example... I read in a value called onoffrange1

    I now want to change its value to an int variable i have called quotient. Here is the syntax I am trying to use:

    reader["onoffrange1"].ToString() = quotient1;

    how do I need to change it?

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

    Re: How to modify a record from reader?

    You don't change data values in a DataReader. A DataReader is for reading data. Once you've read that data you can then do whatever you like with it, but the job of the DataReader itself is done.

    If you want to retrieve, edit and save data in a database then the usual method would be to use a DataAdapter. You call its Fill method to populate a DataTable, edit the data in the DataTable, then call the DataAdapter's Update method to save the changes. The SelectCommand is what controls how the data is retrieved and the UpdateCommand is what will control how it is saved. You can code these to access different tables within the same database if desired, or even different databases.

    If you're only editing a single row then you may instead choose to use a DataReader to retrieve all the data into variables, then close it, then use the ExecuteNonQuery method of another Command to save the edited data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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