mydatatable.comput() question

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
I have a simple windows form that I am trying to total the amount in a table to show up on a label. However, it's not working correctly....

I am using Visual Studio 2008 and the SQL Express Developers Edition that comes with VS. I have a table named "Money Oweds". Within the table I have a field named "AmountOwed".

My datatable is named "mydatatable" and when I use

VB.NET:
Label1.Text = myDataTable.Compute("Sum(MoneyOweds)", "AmountOwed = "")
[\code]

My program stops debugging with errors.

can anyone show me how to do a simple DataTable.Compute syntax?

Thanks
 
Sure, the MSDN documentation can. I have to wonder why you haven't looked there already. Surely if you can't get something to work then the instructions are the first place you should be looking for help. The documentation for the DataTable.Compute method has an explanation, a link to lots of information about syntax and an example. If you have looked at that and you don't understand it then the thing to do would be to explain what you don't understand because just asking for an example when you already have an example that didn't help is a good way to get nowhere.

EDIT: All's wel that ends well. :)
 
I did look at MSDN - as well as plenty of other sites...... I messed up with the code in my previous post....

Revised Code:
VB.NET:
    Me.AmountTableAdapter.Fill(Me.Database1DataSet.Amount)
        Dim table As DataTable
        table = Database1DataSet.Tables("Amount")

        Label1.Text = table.Compute("Sum(MoneyOweds)", Nothing)

I wasn't understanding the Filter until I realized that "Nothing" is for the total column. I tried to cast many different filters, but didn't get it until I came across another site that finally explained it to me....

Sometimes MSDN can be very difficult to read/understand..... I guess the reason why they make it difficult is so you learn without knowing it.... :)

Thanks for your reply
 
MSDN is not intended for the lay person. It is a technical reference for developers. Obviously those with less experience will have more trouble with some aspects of it. That said, when it says:
The second parameter, filter, determines which rows are used in the expression.
I wouldn't think that you need to be an experienced developer to know what that means. The term "filter" is not a programming-specific term so if you know what it means to filter then you should have no real trouble understanding that. Furthermore, nowhere in your first post did you actually say "I don't understand what the 'filter' argument is for". If there is something specific you don't understand then say so. We can't read your mind so you need to explain the actual problem to us. Maybe you didn't know to specify Nothing for the filter if you don't want to filter but, in that case, you should have asked specific what you should pass as the filter when you don't want to filter out any rows and then we could have answered that. As for the fact that your original code apparently contained the table name where it should have had the column name, that is addressed specifically in the documentation. Here's the example:
VB.NET:
Private Sub ComputeBySalesSalesID(ByVal dataSet As DataSet)
    ' Presumes [COLOR="#FF0000"]a DataTable named [B][U]"Orders"[/U][/B][/COLOR] that has [COLOR="#0000FF"]a column named [B][U]"Total."[/U][/B][/COLOR]
    Dim table As DataTable
    table = dataSet.Tables([COLOR="#FF0000"][B][U]"Orders"[/U][/B][/COLOR])

    ' Declare an object variable.
    Dim sumObject As Object
    sumObject = table.Compute("Sum([COLOR="#0000FF"][B][U]Total[/U][/B][/COLOR])", "EmpID = 5")
End Sub
 
Back
Top