none
SqlDataReader over-writing objects RRS feed

  • Question

  • I am reading all rows from an sql server table with multiple columns and rows, displaying certain columns (including the primary key) in a DataGridView.  I am creating a class object for each row as it reads.  After the first read, the first object has the same primary key as the first row in the table.  After the second read, both objects are the same, same as the second row in the table.  After the third read, all objects are the same, same a the third row in the table.  This repeats.  I end up with the same number of rows in the DGV as the table, all rows are the same, same as the last row in the table.  I am trying to figure out if possible, why after having set values for one or more objects, SqlDataReader is overwriting them with values from the latest read.  Has any seen the behaviour?  I have reported the problem in VS.
    Tuesday, February 20, 2018 12:11 AM

Answers

  • Got it working. I avoided using a class object for populating the DGV's.  Filled DGV directly from SqlDataReader.
    Thursday, February 22, 2018 11:48 PM

All replies

  • Maybe you need set the datagridview.ReadOnly=True ,to disable edit 

    BTW, I don't think it's the issue related to Visual Studio IDE

     

    Regards,

    Fletcher  


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Tuesday, February 20, 2018 2:20 AM
    Tuesday, February 20, 2018 2:19 AM
  • Fletcher, I changed datagridview.ReadOnly to true, no affect.  As to your other point, entirely possible.
    Tuesday, February 20, 2018 7:10 AM
  • From the behaviour observed, what seems to be happening is, as the reader reads from the table it is filling objects up to and including the corresponding object with the values of the current row being read, so

    row1: object1

    row2: object1,object2

    row3: object1, object2, object3

    and so on.  The overall effect is all objects end up with the same values, from the last row read.  I'm not say that is what is happening, but it would explain the behaviour though..  I can't think of any logical reason why each object created would end up with the same values, if all rows in the table with a primary key are different.  

    Tuesday, February 20, 2018 4:17 PM
  • What's your code like? Is it possible to share a code sample with us?

     

    Regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Thursday, February 22, 2018 5:27 AM
    Thursday, February 22, 2018 5:27 AM
  • while (Reader.Read()) { Class object= new Class(); object.SetProp1(Reader["Prop1"].ToString()); object.SetProp2(Reader["Prop2"].ToString()); object.SetProp3(Reader["Prop3"].ToString()); object.SetProp4(Reader["Prop4"].ToString()); object.SetProp5(Reader["Prop5"].ToString()); object.SetProp6(int.Parse(Reader["Prop6"].ToString())); object.SetProp7(Reader["Prop7"].ToString()); object.SetProp8(int.Parse(Reader["Prop8"].ToString())); object.SetProp9(int.Parse(Reader["Prop9"].ToString())); object.SetProp10(Reader["Prop10"].ToString()); List.Add(object);

    }


    Thursday, February 22, 2018 2:51 PM
  • Despite that it is strange code (I assume it is C#) this can in my perception not have the behaviour as you write. 

    But you can of course simply debug it. 

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx


    Success
    Cor

    Thursday, February 22, 2018 3:34 PM
  • It is Visual C#.  I have used the step into debug.  At the end of the first read, there is 1 object in the list, with Property1 having the same value as row0 primary key.  On the second read, Property1 in the 2nd object has the same value as the primary key in row1.  As soon as Property1 in object1 has been set I check the value of Property1 in object0, it has changed.  How and/or why, I don't know.  I can only think of 2 possible explanations.  A bug in SqlDataReader or a possibly a memory leak.  The code in the while loop only sets property values of the new(current) object, not objects previously written to.  
    Thursday, February 22, 2018 6:36 PM
  • That bug would mean that now hundred of millions of users would face it, while a memory leak with managed program languages can only happen if unsecure code is used. And that is not what you use. 

    However, the quarter did fall for me. Check your Select code for the DataReader, it seems for me that it returns not what you expect. 

    Be aware that those reports that their code is well but in fact tell that what the programmers from Microsoft made sucks, tell much about themselves. 


    Success
    Cor



    • Edited by Cor Ligthert Thursday, February 22, 2018 6:59 PM
    Thursday, February 22, 2018 6:58 PM
  • The select query string is

    string selectAllFromTable = "select * from TableName";

    I'm only assigning the values from columns that appear in the DGV.  I don't see that as a factor.

    Thursday, February 22, 2018 8:08 PM
  • Got it working. I avoided using a class object for populating the DGV's.  Filled DGV directly from SqlDataReader.
    Thursday, February 22, 2018 11:48 PM