views:

304

answers:

2

I have datatable and there two column which is type of string and I want to make addition of that two columns data for another column how can I do that ?

My column name contains special character and I have used "[" "]" to escaping special character and my columns are type of decimal I am doing

 TableExtractedFromFile.Columns[TOT BROK(Rs)].Expression = 
                "'"+"["+SER TAX(Rs)+"]"+"'+'"+"["+STT(Rs)+"]"+"'";

But The column TOT BROK(Rs) contains the contenation of values of columns SER TAX(Rs) and STT(Rs).But I want the sum of the values of these two columns.

EDIT It works. But If I do like there is three columns A,B and C. Now if do I table.columns["A"].expression = "A+B+C"; then it gives error like Cannot set Expression property due to circular reference in the expression. then what is the solution of that ?

+1  A: 

Use this:

dt.Columns.Add("fullname", typeof(string));
dt.Columns["fullname"].Expression = "lastname + ', ' + firstname";

For adding of value(Total,Amount and Surcharge are your existing columns, and are type string, you need to put CONVERT function in your column names so they will be casted to decimal(System.Decimal), if int use System.Int32 or System.Int64, etc):

dt.Columns["Total"].Expression = 
  "CONVERT(Amount,System.Decimal) + CONVERT(Surcharge,System.Decimal)";

[EDIT]

Just do it in your backend:

    select *, CONVERT(SERTAX(Rs), DECIMAL) 
        + CONVERT(STT(Rs), DECIMAL) AS brokerage 
    from tbl

If you have control in your database, modify the SERTAX and STT function so it can return decimal/currency/int, not string.

Then in your front-end, do this:

dt.ColumnChanging += (ds, de) =>
{
    if (de.Column.ColumnName == "Rs")
    {
        decimal serTaxResult = (decimal)new SqlCommand("select SERTAX(" + de.ProposedValue + ")").ExecuteScalar();
        decimal sttResult = (decimal)new SqlCommand("select STT(" + de.ProposedValue + ")").ExecuteScalar();

        // if your SERTAX AND STT really return string, use this:                        
        // decimal serTaxResult = decimal.Parse( (string) new SqlCommand("select SERTAX(" + de.ProposedValue + ")").ExecuteScalar() );
        // decimal sttResult = decimal.Parse( (string) new SqlCommand("select STT(" + de.ProposedValue + ")").ExecuteScalar() );


        de.Row["brokerage"] = serTaxResult + sttResult;
    }
};

[EDIT]

If all your columns are string type, you have to enclosed each of them with their own CONVERTs.

.Expression = string.Format("CONVERT({0},System.String)", 

      "CONVERT(" + serviceTaxClmnInCNote  + ", System.Int32)"
      + " + " 
      + "CONVERT(" + STTClmnInCNote + ", System.Int32)"

      );

Just change the System.Int32 to System.Decimal if the serviceTax and STT are money type.

Michael Buen
@Michael Buen,Will this make addition or concatenate two strings ?
Harikrishna
if any of the field is string, it will result to concatenation. but if they are both numbers, it will result to addition
Michael Buen
@Micheal,These two columns are type of string not int and I can not change the datatype of them, because it gives error like can not change the datatype once data is inserted.
Harikrishna
here are the list of possible expressions http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(VS.71).aspx
Michael Buen
@Micheal, I have seen that, but can not change the datatype of the columns.
Harikrishna
@Harikrishna: just omit the dt.Columns.Add, assign an expression to dt.Columns["yourExistingColumnHere"]'s Expression
Michael Buen
@Harikrishna: i'll edit my answer
Michael Buen
@Micheal,If instead of writing like "amount+Surcharge" I write `dt.columns["Amount"].Caption+dt.columns["SurCharge"].Caption;` it gives error.
Harikrishna
@Micheal,Error is missing operator.
Harikrishna
@Harikrishna: you cannot assign the columns caption, doing that will resolve things in compile time. just assign it like an ordinary expression
Michael Buen
@Micheal, I have to do that because I can access the column name dynamically.
Harikrishna
@Harikrishna: but caption is not integer, and caption is for data grid header purposes only. if you want to achieve adding of values on your existing column, just assign it to expression
Michael Buen
@Micheal,It gives error like missing operator when I use expression only.
Harikrishna
@Harikrishna: kindly put in your question what have you done. in verbatim, so i can see your code's intent
Michael Buen
@Micheal,Ok I will edit my question. and sorry for that.
Harikrishna
@Harikrishna: by the way, what happen when you do: `dt.Columns["Total"].Expression = "CONVERT(Amount,System.Decimal) + CONVERT(Surcharge,System.Decimal)";` ?
Michael Buen
@Micheal,It gives error like `The expression contains undefined function call SERTAX().`
Harikrishna
@Harikrishna: see my comment in your question
Michael Buen
@Micheal,My data in the datatable is coming from the file by parsing it through html agility pack.
Harikrishna
@Harikrishna: ok, now I know. where does the SERTAX and STT function come from? if from your C#, just change the `de.Row["brokerage"].Value` assignment. see my edited answer. ColumnChanging also triggers even while you are adding rows to your DataTable
Michael Buen
@Micheal,Do you mean that when I inserting records in the datatable ?
Harikrishna
@Harikrishna: Is the DataTable populated by you? or is it merely returned from HTML Agility Pack?
Michael Buen
@Micheal,Ok I have put a condition that if these columns are coming in the table then it will type of int.Now my column names are assigned to string variable and I am doing like `TableExtractedFromFile.Columns[brokerageClmnInCNote].Expression ="'"+serviceTaxClmnInCNote+"'+'"+STTClmnInCNote+"'";` But problem is because of string variable it gives error that can not convert in int32.
Harikrishna
if your brokerageClmnInCNote is string type. try: `.Expression = string.Format("CONVERT({0},System.String)", serviceTaxClmnInCNote + " + " + STTClmnInCNote)`
Michael Buen
if all your columns are string type, i'll just put the answer in my edit. check my edit later
Michael Buen
@Micheal,Name of Column **ServiceTax** is assigned to **serviceTaxClmnInCNote** variable and that column is type of int.And Name of column **STT** is assigned to **STTClmnInCNote** and type of int.
Harikrishna
@Micheal,I have tried as you described in your edited answer but it gives error like : **The expression contains undefined function call SERTAX().**
Harikrishna
where is SERTAX defined?
Michael Buen
serviceTaxClmnInCNote is a string variable within which SERTAX is stored which is the column name.
Harikrishna
if SERTAX is a C# function, forgot the Expression, it cannot process functions that are not built-in in Expression's own language. use the ColumnChanging event. for proof of concept code, try to work by piecemeal approach. first, make the ColumnChanging event assign values to computed column like this: `de.Columns["result"].Value = 7 * 6`. then replace it one-by-one with your expression/functions
Michael Buen
@Micheal,Column Name is returned by html agility pack and I stored that column name in the string variable serviceTaxClmnInCNote.
Harikrishna
@Micheal,Now it works but the result is string concatenate not addition.
Harikrishna
@Micheal,I know the reason why it does not work,because column name contains special character.
Harikrishna
A: 

Extract the data from te two columns and call the string concat function. Refer the following link

http://www.1keydata.com/sql/sql-concatenate.html

Pavan Navali
@Pavan,I have need of making addition of two columns data not concat.
Harikrishna