DataSet Clear Probs ? new to .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • polecat
    New Member
    • Jan 2007
    • 5

    DataSet Clear Probs ? new to .net

    Im new to .NET and I am having probs with this
    Every time I reload the form the same data is repeated again....
    Done reading on msdn and another forum
    cant figure out why the DsProds.Tables( 0).Clear() is not
    clearing the first lot of data ?


    Code:
    ' This is in my load event
    
      Dim DsProds As New DataSet
            Dim DsProdsTable As New DataTable("Products")
            DsProds.Tables.Add(DsProdsTable)
            DsProds.Tables(0).Clear()
    
            'DataAdapter fill DataSet
    
            Dim connectionString As String = _
        My.Settings.StaticCartSQLConnectionString
            Dim connection As SqlConnection = _
               New SqlConnection(connectionString)
            connection.Open()
    
            Dim command As SqlCommand = _
               New SqlCommand("SELECT * FROM Products", connection)
            Dim ProdsAdapter As SqlDataAdapter = New SqlDataAdapter(command)
    
            ProdsAdapter.Fill(DsProds)
    
            TextBox1.Text = DsProds.Tables.Count()
    
            Dim NewRow As DataRow
            For Each NewRow In DsProds.Tables(0).Rows
           
                ' to add as string to the names of controls
                Dim ProdID
                ProdID = NewRow.Item("Prod_ID").ToString
                '
                'ProdLabel
                '
                Const ProdLabel_HEIGHT As Integer = 13
                Const ProdLabel_WIDTH As Integer = 39
                Dim ProdLabel As New Label
                ProdLabel.Anchor = System.Windows.Forms.AnchorStyles.None
                ProdLabel.AutoSize = True
                ProdLabel.Location = New Point(ProdLabelcontrolLocation.X, ProdLabelcontrolLocation.Y)
                ProdLabel.Name = "ProdLabel," + ProdID
                ProdLabel.Height = ProdLabel_HEIGHT
                ProdLabel.Width = ProdLabel_WIDTH
                ProdLabel.Text = NewRow.Item("Prod_Desc")
                'Add the label to the controls collection.
                Controls.Add(ProdLabel)
                ' Increment the LocationY so the next control won't overwrite it.
                ProdLabelcontrolLocation.Y += ProdLabel.Height + 1
                '
                'more controls
                'more controls
                'Cut the controls code down as that is not the issue
    Next
    Any I mean any help or suggestion welcome 2 days and this has done my head in lol!!!
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Where do you update the products table? From your code, I would expect the results to be the same each time the page loads...

    Comment

    • polecat
      New Member
      • Jan 2007
      • 5

      #3
      I have a Seperate form for adding and editing existing products.
      The form in question simply displays labels in a table layout panel
      for each product in the products table in the database.
      The results never clear and are repeated every time the form loads?

      Comment

      • radcaesar
        Recognized Expert Contributor
        • Sep 2006
        • 759

        #4
        Try by put this code block inside

        if !(Page.IsPostba ck)

        :)

        Comment

        • polecat
          New Member
          • Jan 2007
          • 5

          #5
          Try by put this code block inside

          if !(Page.IsPostba ck)
          Isnt this for ASP.NET ?
          cant use postback in winforms.....

          Really could do with some help on this situation

          Comment

          • kenobewan
            Recognized Expert Specialist
            • Dec 2006
            • 4871

            #6
            My initial concern was that your update was not working. Please confirm that the values have changed in the database. If the update works, I would try deleting DsProdsTable, unless you use it elsewhere, and use DsProds.clear() without the reference to the table...

            Comment

            • polecat
              New Member
              • Jan 2007
              • 5

              #7
              Im not doing any thing other than displaying the data in controls

              DsProds.clear() does not work
              DsProds.Dispose () does not work
              and
              DsProds = Nothing throws null reference exeption

              Comment

              • bplacker
                New Member
                • Sep 2006
                • 121

                #8
                I always clear out my variables at the end of a function, and i just say DataSet = nothing, or whichever type of table you are using, and I've never had a problem with it. It would only throw a null reference exception if the data table was already null and you were trying to access it.

                Comment

                Working...