How do i set DataRow class to last row of a Sql table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pugalenthi
    New Member
    • Jan 2007
    • 44

    How do i set DataRow class to last row of a Sql table

    I have to add a value to an empty field in the last row of the table using C#.

    What changes should i make to the following code so that i can set 'thisRow' to last row in the table rather than to a new row.

    DataRow thisRow = thisDataSet.Tab les["Table"].NewRow();
    thisRow["Exit_time"] = Exittime;
    thisDataSet.Tab les["Table"].Rows.Add(thisR ow);
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Rows contain data. If they don't, they aren't real.
    A "blank row" at the end of the table is just a template for filling in a new row.

    Unluess you allow nulls in every column and create a row with nothing but nulls in it and don't have any auto-incrementing fields and no primary key.

    Comment

    • pugalenthi
      New Member
      • Jan 2007
      • 44

      #3
      Originally posted by Plater
      Rows contain data. If they don't, they aren't real.
      A "blank row" at the end of the table is just a template for filling in a new row.

      Unluess you allow nulls in every column and create a row with nothing but nulls in it and don't have any auto-incrementing fields and no primary key.
      I guess you misunderstood what i said. In my table the last row has data in every column except for 1 column, which allows null. And now i need to insert value into this column in the last row. The value of this column will be a output of the c# program.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Scracth that.
        You want to enter the data into the last row of a datatable in code?

        You should be able to do this?
        Code:
        int lastrowidx=thisDataSet.Tables["Table"].Rows.Count-1;
        DataRow thisRow = thisDataSet.Tables["Table"].Rows[lastrowidx];
        thisRow["Exit_time"] = Exittime;

        Comment

        • pugalenthi
          New Member
          • Jan 2007
          • 44

          #5
          Originally posted by Plater
          Scracth that.
          You want to enter the data into the last row of a datatable in code?

          You should be able to do this?
          Code:
          int lastrowidx=thisDataSet.Tables["Table"].Rows.Count-1;
          DataRow thisRow = thisDataSet.Tables["Table"].Rows[lastrowidx];
          thisRow["Exit_time"] = Exittime;
          I tried the above code in my program, and finally used the following lines,

          thisDataSet.Tab les["Table"].Rows.Add(thisR ow);
          thisAdapter.Upd ate(thisDataSet , "Table");

          while i executed the code i got two exception,

          System.Argument Exception occured in System.Data.dll
          System.Reflecti on.TargetInvoca tionException occured in mscorlib.dll

          I was not able to update the column of last row with the output of my program.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Oh, I thought the Row was already IN the DataTable.

            What part is triggering the argument exception?

            Comment

            • pugalenthi
              New Member
              • Jan 2007
              • 44

              #7
              Originally posted by Plater
              Oh, I thought the Row was already IN the DataTable.

              What part is triggering the argument exception?
              int lastrowidx=this DataSet.Tables["Table"].Rows.Count-1;
              DataRow thisRow = thisDataSet.Tab les["Table"].Rows[lastrowidx];
              thisRow["Exit_time"] = Exittime;
              thisDataSet.Tab les["Table"].Rows.Add(thisR ow);
              thisAdapter.Upd ate(thisDataSet , "Table");

              The first 3 lines of the code working perfectly fine. The variable Exittime's value gets passed to thisRow["Exit_time"]. I guess the problem is in the last two lines.

              I need to just insert value into one column of the last row, whose other columns are already filled.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Is there a column with that name in the datatable?

                Comment

                • pugalenthi
                  New Member
                  • Jan 2007
                  • 44

                  #9
                  Originally posted by Plater
                  Is there a column with that name in the datatable?
                  Yes there is a column named Exit_time.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    I take it that it's sqldatatype is that of "datetime" is the "exittime" variable you're trying to set it also a datetime object?


                    And to make sure we're on the same page:
                    int lastrowidx=this DataSet.Tables["Table"].Rows.Count-1;
                    DataRow thisRow = thisDataSet.Tab les["Table"].Rows[lastrowidx];
                    If you've done the above, I believe this line here will error:
                    thisDataSet.Tab les["Table"].Rows.Add(thisR ow);


                    You are in fact using the code you created and not mine (like above)?

                    Comment

                    • pugalenthi
                      New Member
                      • Jan 2007
                      • 44

                      #11
                      Originally posted by Plater
                      I take it that it's sqldatatype is that of "datetime" is the "exittime" variable you're trying to set it also a datetime object?


                      And to make sure we're on the same page:
                      int lastrowidx=this DataSet.Tables["Table"].Rows.Count-1;
                      DataRow thisRow = thisDataSet.Tab les["Table"].Rows[lastrowidx];
                      If you've done the above, I believe this line here will error:
                      thisDataSet.Tab les["Table"].Rows.Add(thisR ow);


                      You are in fact using the code you created and not mine (like above)?
                      I have set datatype to datetime in the database.

                      I dont get any error, i just get those 2 exceptions i have specified before.

                      I was able to enter into the database values for other columns during another event. During that event i used the following code to insert data into new row,

                      DataRow thisRow = thisDataSet.Tab les["Table"].NewRow();

                      The only difference i face in this event handler is that i need to insert data into 1 column of the last row.

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        DataRow thisRow = thisDataSet.Tab les["Table"].NewRow();
                        Creates a NEW row, and then you add it into the datatable with
                        this.DataSet.Ta bles["Table"].Rows.Add(thisR ow);

                        That's fine.

                        I was saying that if you pick a row already in the datatable and try to add it again, it will error.


                        You already have this:
                        idx | col1 | col2 | col3 | exittime
                        ---------------------------------------------------
                        1 | data | data | data | (empty)
                        2 | data | data | data | (empty)
                        3 | data | data | data | (empty)
                        4 | data | data | data | (empty)

                        And you would just want to go to row 4(the last row) and add the datetime to the exittime column:
                        idx | col1 | col2 | col3 | exittime
                        ---------------------------------------------------
                        1 | data | data | data | (empty)
                        2 | data | data | data | (empty)
                        3 | data | data | data | (empty)
                        4 | data | data | data | (a datetime that you specify)

                        right?

                        Comment

                        • pugalenthi
                          New Member
                          • Jan 2007
                          • 44

                          #13
                          Originally posted by Plater
                          DataRow thisRow = thisDataSet.Tab les["Table"].NewRow();
                          Creates a NEW row, and then you add it into the datatable with
                          this.DataSet.Ta bles["Table"].Rows.Add(thisR ow);

                          That's fine.

                          I was saying that if you pick a row already in the datatable and try to add it again, it will error.


                          You already have this:
                          idx | col1 | col2 | col3 | exittime
                          ---------------------------------------------------
                          1 | data | data | data | (empty)
                          2 | data | data | data | (empty)
                          3 | data | data | data | (empty)
                          4 | data | data | data | (empty)

                          And you would just want to go to row 4(the last row) and add the datetime to the exittime column:
                          idx | col1 | col2 | col3 | exittime
                          ---------------------------------------------------
                          1 | data | data | data | (empty)
                          2 | data | data | data | (empty)
                          3 | data | data | data | (empty)
                          4 | data | data | data | (a datetime that you specify)

                          right?
                          Yes, i want to do exactly the same.

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            Originally posted by pugalenthi
                            Yes, i want to do exactly the same.
                            If the row is already IN the table, you do NOT want to use .NewRow()

                            Which is why I gave you this:

                            Code:
                            int lastrowidx=thisDataSet.Tables["Table"].Rows.Count-1;//the index of the last row in the table
                            DataRow thisRow = thisDataSet.Tables["Table"].Rows[lastrowidx];//the last row in the table
                            thisRow["Exit_time"] = Exittime;//set the Exit_time column to the DateTime object "Exittime"

                            Comment

                            • pugalenthi
                              New Member
                              • Jan 2007
                              • 44

                              #15
                              Originally posted by Plater
                              If the row is already IN the table, you do NOT want to use .NewRow()

                              Which is why I gave you this:

                              Code:
                              int lastrowidx=thisDataSet.Tables["Table"].Rows.Count-1;//the index of the last row in the table
                              DataRow thisRow = thisDataSet.Tables["Table"].Rows[lastrowidx];//the last row in the table
                              thisRow["Exit_time"] = Exittime;//set the Exit_time column to the DateTime object "Exittime"
                              I didnt use NewRow(). I used Row.Count-1 as specified in your code. Only on another occasion i used NewRow(), were i wanted to enter data in a new row.

                              Comment

                              Working...