Adryan Permana
Adryan Permana

Reputation: 127

how to add new column in datatable asp.net mvc

I have this code:

public ActionResult RcColdis() {
string constr = @"Data Source=192.168.52.197,1433;Network Library=DBMSSOCN;Initial Catalog=DB_ADRIAN;User ID=adrian;Password=password;";

using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("select a.secCode, b.[Haircut (%)],c.[Absolute IDR (Miliar)] from openquery(PDC_MYAPPS,'select secCode from mii.secReq')a left join (select * from RC_Saham)b on a.secCode=b.kode left join (select * from Absolute_IDR)c on b.kode=c.[Kode Saham]where b.[Haircut (%)] is not null union select a.code_Ic,a. PERC_HRCT, case  a. PERC_HRCT when 100 then 20 end from openquery(MANTARAY,'select b.code_ic, a.PERC_HRCT from cminstruments a, instrument_codes b where a.ID_CMI_CAPCO = b.INS_ID_INS_CAPCO')a left join (select * from Absolute_IDR)b on a.code_Ic = b.[Kode Saham]where code_Ic in (select a.secCode from openquery(PDC_MYAPPS,'select secCode from mii.secReq')a left join (select * from RC_Saham )b on a.secCode=b.kode left join (select * from Absolute_IDR)c on b.kode = c.[Kode Saham] where c.[Absolute IDR (Miliar)] is null )"))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;

            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);

                //Build the Text file data.
                string txt = string.Empty;

                //Add new line.
                foreach (DataRow row in dt.Rows)
                {
                     foreach (DataColumn column in dt.Columns)
                     {
                         //Add the Data rows.
                         txt += row[column.ColumnName].ToString() + "|";
                     }

                     //Add new line.
                     txt += "\r\n";
                }

                //Download the Text file.
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=SqlExport.txt");
                Response.Charset = "";
                Response.ContentType = "application/text";
                Response.Output.Write(txt);
                Response.Flush();
                Response.End();
            }
       }
    }
}  

return View("Index");
}

It will export data from database to datatable and then txt file. I got the result which consist of two columns:

asdasd|12112
sdfsfs|2342
asdsgf|3455
gfhghj|87897
ghjghj|45654

I want to add new column in the middle with value '10' for all rows. How can I do it?

Upvotes: 0

Views: 931

Answers (1)

Shyju
Shyju

Reputation: 218722

If you want the constant value 10 for all your records, you can update your SQL query to return that value as a column.

"select a.secCode,10 as MyValue, b.[Haircut (%)],c.[Absolute IDR (Miliar)] from openquery"

Upvotes: 1

Related Questions