Results 1 to 5 of 5

Thread: get maximum value of a dataset

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Posts
    778

    get maximum value of a dataset

    Hello, I am looking for how to get the maximum value record on my dataset. I found this code
    Code:
    (maximumRecord = myDataset.compute("Max(columnName)", ""))
    on google but this is not working with a dataset, it is suppose to work on a table, but I need to get the maximum value of a specific data that I put into a dataset.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: get maximum value of a dataset

    DataSets don't have maximum values. In fact, they have no values at all. DataSets contains DataTables. It's the DataTables that contain the columns that describe the data and the rows that contain the data. You can't possibly have put any data directly into a DataSet because it's simply not possible. You have to have put the data into a DataTable, so just call Compute on that DataTable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Posts
    778

    Re: get maximum value of a dataset

    well, I can't just call the whole table because I need to calculate the maximum value of a specific data, I'll show you what I got. I am trying to use the maximum value to make a primary key.


    Code:
    Dim maximumRecord As String
        Private Sub Load_History()
    
            Dim fontname As String = "Tahoma"
            Dim fontsize As Integer = "8"
            Dim fnt As New Font(fontname, fontsize, FontStyle.Regular)
    
            Dim myCnt As Integer
    
            Dim myStr As String
    
            Dim myDataset As New DataSet
    
            myStr = "select * from notes where Orderref='" + txtorderref.Text + "'"
    
            Dim sqldaHistory As New SqlDataAdapter(myStr, nwindconn)
    
            sqldaHistory.Fill(myDataset, "notes")
    
            myCnt = myDataset.Tables(0).Rows.Count
    'can't use noteCounter which works but would cause me errors if a row gets deleted, myDataset.Tables(0).Rows.Count method not suitable for PKs
            'noteCounter = myDataset.Tables(0).Rows.Count
    
    
            'maximumRecord = myDataset.compute("Max(noteref)", "")
         
    
            Do While myCnt > 0
                myCnt = myCnt - 1
    
                Dim lblName As New Label
                'name is not yet on notes table, to be copied from orders owner
                lblName.Text = myDataset.Tables(0).Rows(0).Item("name")
                lblName.Font = fnt
    
                Dim lblEvent As New Label
                lblEvent.Size = New Size(134, 17)
                'event is on notes but most of the records are empty
                lblEvent.Text = myDataset.Tables(0).Rows(0).Item("Event").ToString
                lblEvent.Font = fnt
    
                Dim lblNote As New Label
                lblNote.Text = myDataset.Tables(0).Rows(0).Item("note")
                lblNote.Size = New Size(1105, 39)
                lblNote.Font = fnt
                lblNote.AutoSize = True
    
                pnlNewNote.Controls.Add(lblName)
                pnlNewNote.Controls.Add(lblEvent)
                pnlNewNote.Controls.Add(lblNote)
    
            Loop
    
        End Sub
    So I want to get the maximum value from these records:
    Code:
    myStr = "select * from notes where Orderref='" + txtorderref.Text + "'"

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Posts
    778

    Re: get maximum value of a dataset

    I just tried this but I got an error:
    An expression of non-boolean type specified in a context where a condition is expected, near 'M0800176 '.

    Code:
    Dim db_command As New SqlCommand("select max(NoteRef) as max_NoteRef from notes where '" + txtorderref.Text + "'", nwindconn)
            Label1.Text = db_command.ExecuteScalar()

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Posts
    778

    Re: get maximum value of a dataset

    That's what I been looking for
    Code:
    maximumRecord = myDataset.Tables(0).Compute("Max(noteref)", "")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width