how to alter values in access database table using C# without adding new rows?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asmi
    New Member
    • Jul 2010
    • 4

    #1

    how to alter values in access database table using C# without adding new rows?

    I am new in C# programming. Can anyone plz help me in the following problem.
    I am changing values in different columns in access database table using c# but these changes are only visible at run time. I want to save these changes in the table without adding new rows, just want to replace old values with new ones. I'v used the "adapter.Update " command but it does not work.

    Plz reply me
    thankx in advance
  • Christopher Nigro
    New Member
    • Jul 2010
    • 53

    #2
    Please show what you tried and clarify how it "does not work".

    Comment

    • asmi
      New Member
      • Jul 2010
      • 4

      #3
      Originally posted by Christopher Nigro
      Please show what you tried and clarify how it "does not work".
      Actually this is what I am doing. this is only a chunk of my main code. "attrib_num " contains the column number where i want to insert new value in the table and "marked_attribu te_dec" is the new value which i want to add in the specified column number replacing the old value.
      but the code below adds new rows with the new values in the table. i want the same rows with the new values in them. In other words i want to replace some of the values of the original table rows with new column values. I dont want to have additional rows with new values. i just want to alter values in the existing rows.
      please give me some suggestions. I really need assistance


      [code.......

      DataRow row1 = ds.Tables["Table1"].NewRow();


      for (int p = 0; p <column_count ; p++)
      {
      if (p == attrib_num)
      row1[p]=marked_attribu te_dec;
      else

      row1[p] = attributes[p];
      }


      ds.Tables["Table1"].Rows.Add(row1) ;
      conn.Close();


      DataTable dt = ds.Tables["Table1"];
      adapterx.Update (dt);
      conn.Close();
      ............... ............... .code]


      thanku

      Comment

      • Christopher Nigro
        New Member
        • Jul 2010
        • 53

        #4
        Instead of creating a new row via NewRow(), you need to update a particular row. How do you know which row(s) need to be updated? Are the new values coming from user input in the UI or are you updating rows programatically where the rows fit some predefined criteria? Basically, you need to tell me which rows need to be updated before I can understand what you are trying to accomplish.

        Comment

        • asmi
          New Member
          • Jul 2010
          • 4

          #5
          Actually I am randomly generating some numbers that indicate what specific columns or attributes of a particular row(or rows) need to be updated.
          The new values are not coming from user input I am doing it programatically . My problem is how I can update those particular rows and columns with the new values I am generating.
          I dont know any command for that thats why I have created new rows to make sure that my code is generating new values and iserting them at the specified row and column number correctly otherwise I can only see the changed values at run time not in the table.

          I just want to know how to update existing values with new values in access table. My requirement is to update values not create extra rows which I created because I didnt have any other choice.

          Comment

          • asmi
            New Member
            • Jul 2010
            • 4

            #6
            Hi Christopher,
            I'v done it myself.
            It was simple but it made me do alot of effort.
            Heres the code

            Code:
            for (int p = 0; p < column_count; ++p)
                            {
            
                                if (p == attrib_num)
            
            
                       ds.Tables["Orders2"].Rows[m][p] = marked_attribute_dec;
            
            
                            }
                             
                        }
            DataTable dt = ds.Tables["Orders2"];
            adaptery2.Update(dt);
            conn1.Close();

            Here "attrib_num " contains the column no. and"marked_attr ibute_dec" contains the value i wanted to replace in my table at the specified column no.(attrib_num) .
            Thanks you replyed me and found out time to consider my problem and made me consider my own logic.

            Comment

            Working...