tableadapter query

I am using a dataset and tableadapter with the following query

SELECT FinCustomerCode, SUM(FinCurrentP01 + FinCurrentP02 + FinCurrentP03) AS Expr1
FROM T_Financial
GROUP BY FinCustomerCode
HAVING (FinCustomerCode = ?)

How do I get the value of Expr1

[280 byte] By [Can-Ann] at [2007-12-26]
# 1


The following assumes the name of your TableAdapter is T_FinancialTableAdapter1:

Dim T_FinancialTable As DataTable = Me.T_FinancialTableAdapter1.GetData()
Console.WriteLine(T_FinancialTable.Rows(0)("Expr1").ToString)

PaulPClementIV at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2
How would this be done in c#?
Can-Ann at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3

DataTable T_FinancialTable = this.T_FinancialTableAdapter1.GetData();
Console.WriteLine(T_FinancialTable.Rows[0]["Expr1"].ToString);
PaulPClementIV at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4

Sorry it took so long getting back to this : but this does not seem to work

DataTable DT_FinancialTable = this.t_FinancialTableAdapter.FillByCFQ (this.custCustomerCodeTextBox.Text);

Console.WriteLine(DT_FinancialTable.Rows[0]["Expr1"].ToString());

The first line causes this error -

Cannot implicitly convert type 'string' to 'System.Data.DataTable'

Can-Ann at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 5


I'm not exactly sure what you're trying to do here. The FillBy method requires a DataTable as the first parameter. The second parameter is the actual value you want to filter by.

FillBy is typically used with a DataSet/DataTable that is created using the designer, although you could use a DataSet/DataTable that was created in code. See the following for more info:

How to: Create TableAdapter Queries

PaulPClementIV at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 6

The FillByCFQ was created using the designer in VS 2005

which created the following sql

SELECT FinCustomerCode, SUM(FinCurrentP01 + FinCurrentP02 + FinCurrentP03) AS Expr1
FROM T_Financial
GROUP BY FinCustomerCode
HAVING (FinCustomerCode = ?)

What I want to know is how to read the value of Expr1 ?

Can-Ann at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 7

Assuming that you are using a scalar query (returning a single value) then you could do following:

class GetMyValue
{
public double mySumValue(string FinCustomerCode)
{
try
{
YourDataSetTableAdapters.T_FinancialTableAdapter a;
a = new YourDataSetTableAdapters.T_FinancialTableAdapter();
double amount = 0;
if (T_FinancialTableAdapter.FillByCFQ( FinCustomerCode == null)
{
return 0;
}
else
{
amount = (double)cT_FinancialTableAdapter.FillByCFQ(FinCustomerCode);
return (double)amount;
}
catch (Exception ex)
{
return 0;
}
}

then getting the value is as follows

GetMyValue g = new GetMyValue();

double myValue = g.mySumValue("some customer code here");

this is air code and makes some assumptions (like FinCustomerCode is a string), but this is what I do...

steveareno at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 8

That doesn't work for me - the query returns two fields

(FinCustomerCode, SUM(FinCurrentP01 + FinCurrentP02 + FinCurrentP03) AS Expr1)

the finCustomer Code and Expr1. - Expr1 is what I want to know.

I should be able to access the tableadapter row - I just dont know the format.

Can-Ann at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 9

ok. however, since you are passing FinCustomerCode in as a parameter, don't you already know it's value? The only reason (technically speaking) I can see including more than the agg field field in your query is because you expect more than one result based on your groupings. So, for instance:

@customer_id = '11111'

select customer_id, sum(amount) from orders group by customer_id having customer_ID=@customer_id

should only return one row-for the customer id you passed in. basically including the customer_id value in the result set above is (technically, again) meaningless because it will only return that customer anyway. however, if you did this:

@customer_type='something'

select customer_type, customer_id, sum(amount) from orders group by customer_type, customer_id having customer_type=@customer_type

this could return more than one row, therefore, if you were looking for a specific value in a specific row, you would need to know the location/index of the row, and you could do something like this:

string customerCode = "";
double amount = 0;
yourDataSetName ds = new yourDataSetName();
yourDataSetTableAdapters.t_FinancialTableAdapter t;
t = new yourDataSetTableAdapters.t_FinancialTableAdapter();
t.FillByCFQ(ds.t_Financial,this.custCustomerCodeTextBox.Text);
DataView dv = new DataView(ds.t_Financial);
int i = dv.Count;
if (i > 0)
{
if (dv[0][0].ToString().Length > 0)
{
customerCode = dv[0][0].ToString();
}
if (dv[0][1].ToString().Length > 0)
{
amount = dv[0][1].ToString();
}
}

There may be less wordy ways of doing this, such as Pauls suggestion. One of the reasons you were getting an error with Pauls example is because you have to provide a DataSet.DataTable in his example:

DataTable DT_FinancialTable = this.t_FinancialTableAdapter.FillByCFQ (DataSet.DataTable, this.custCustomerCodeTextBox.Text);


steveareno at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified