locked
Adding row to a data Table RRS feed

  • Question

  • I've been working on this for about four hours now and I'm unable to get things working.  I want to add row to an data Table.  I continuelly get the message "Data row already belongs to this table" What am I doing wrong here sample below:

     

    Dim AstroDataSet As New DataSet("AstroData")

    Dim Astrobl As DataTable = AStroDataSet.Tables.Add("AstroTable")

    Dim Rec_YearColumn As DataColumn = New DataColumn("Rec_Year", System.Type.GetType("System.Int32"))

    Dim Rec_MonthColumn As DataColumn = New DataColumn("Rec_Month", System.Type.GetType("System.Int32"))

    Dim Rec_TypeColumn As DataColumn = New DataColumn("Rec_Type", System.Type.GetType("System.String"))

    Dim Member_IDColumn As DataColumn = New DataColumn("Member_ID", System.Type.GetType("System.String"))

    AstroTbl.Columns.Add(Rec_YearColumn)

    AstroTbl.Columns.Add(Rec_MonthColumn)

    AstroTbl.Columns.Add(Rec_TypeColumn)

    AstroTbl.Columns.Add(Member_IDColumn)

    Dim PDEReader As SqlDataReader = SelectCommand.ExecuteReader

    If PDEReader.HasRows Then

    Do While PDEReader.Read

    AstroRow("Rec_Year") = PDEReader.Item("Rec_Year")

    AstroRow("Rec_Month") = PDEReader.Item("Rec_Month")

    AstroRow("Rec_Type") = PDEReader.Item("Rec_Type")

    AstroRow("Member_ID") = Trim(PDEReader.Item("Member_ID").ToString)

    AstroTbl.Rows.Add(AstroRow)

    Loop

    End If

    Why doe sthe above code not work? 

     

    Thanks

     

     

    Monday, May 1, 2006 8:59 AM

Answers

  • Create a new row each time the loop runs otherwise you keep trying to add the same row to the table

     

    Do While PDEReader.Read

    dim AstroRow as datarow=Astrobl.NewRow

    AstroRow("Rec_Year") = PDEReader.Item("Rec_Year")

    AstroRow("Rec_Month") = PDEReader.Item("Rec_Month")

    AstroRow("Rec_Type") = PDEReader.Item("Rec_Type")

    AstroRow("Member_ID") = Trim(PDEReader.Item("Member_ID").ToString)

    AstroTbl.Rows.Add(AstroRow)

    Loop

    Monday, May 1, 2006 9:30 AM
    Moderator

All replies

  • Create a new row each time the loop runs otherwise you keep trying to add the same row to the table

     

    Do While PDEReader.Read

    dim AstroRow as datarow=Astrobl.NewRow

    AstroRow("Rec_Year") = PDEReader.Item("Rec_Year")

    AstroRow("Rec_Month") = PDEReader.Item("Rec_Month")

    AstroRow("Rec_Type") = PDEReader.Item("Rec_Type")

    AstroRow("Member_ID") = Trim(PDEReader.Item("Member_ID").ToString)

    AstroTbl.Rows.Add(AstroRow)

    Loop

    Monday, May 1, 2006 9:30 AM
    Moderator
  •  

    Goodness... does that ever look familiar!!!!

    Monday, May 1, 2006 7:28 PM