Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have and sql trigger insert/update statement on location master.
I have inserted the trigger data into locationmasterhistory table to track the inserted stocks.

I have created a console app, to get the locationmasterhistory tables data to be exported into excel. In the locationmasterhistory , there is a column export = "No". So When i export the data into excel, in the coding I want to change the column to "Yes". Please assist me.

What I have tried:

This is my code in the console application :

static void Main(string[] args)
        {
            
            string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["IntegrationConnection"].ConnectionString;

            //Location Sheet
            string sql1 = null;
            string data = null;
            int i = 0;
            int j = 0;
            string Filename = @"D:\Integration\Location" + DateTime.Now.ToString("ddMMyyyy HHmmss") + ".xls";
            //string Filename = @"D:\Integration\Location.xls";
            if (!File.Exists(Filename))
            {
                File.Create(Filename).Dispose();
                using (TextWriter tw = new StreamWriter(Filename))
                {
                    tw.WriteLine("Please run the program again");
                    tw.Close();
                }
            }

            ////*** Preparing excel Application
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            ///*** Opening Excel application
            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(Filename);
            xlWorkSheet = (Excel.Worksheet)(xlWorkBook.ActiveSheet as Excel.Worksheet);

            xlApp.DisplayAlerts = false;
            SqlConnection conn = new SqlConnection(connectionstring);
            conn.Open();

            sql1 = "SELECT * from tblCompBranchHistory where export = 'N'";

            ///*** Preparing to retrieve value from the database
            DataTable dtable = new DataTable();

            SqlDataAdapter dscmd = new SqlDataAdapter(sql1, conn);
            DataSet ds = new DataSet();
            dscmd.Fill(dtable);

            ////*** Generating the column Names here
            string[] colNames = new string[dtable.Columns.Count];
            int col = 0;

            foreach (DataColumn dc in dtable.Columns)
                colNames[col++] = dc.ColumnName;

            char lastColumn = (char)(65 + dtable.Columns.Count - 1);

            xlWorkSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
            xlWorkSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;

            xlWorkSheet.get_Range("A1", lastColumn + "1").VerticalAlignment
                               = Excel.XlVAlign.xlVAlignCenter;

            /////*** Inserting the Column and Values into Excel file
            for (i = 0; i <= dtable.Rows.Count - 1; i++)
            {
                for (j = 0; j <= dtable.Columns.Count - 1; j++)
                {
                    data = dtable.Rows[i].ItemArray[j].ToString();
                    xlWorkSheet.Cells[i + 2, j + 1] = data;
                    xlWorkBook.Save();
                }
            }

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);


}
Posted
Updated 5-Aug-20 6:08am
v2
Comments
Garth J Lancaster 3-Aug-20 23:51pm    
Its not immediately obvious where locationmasterhistory is .. your code seems to show you loading from database table tblCompBranchHistory to a data-table to excel.

The obvious answer, in general, is, use the unique ID's from the data-table for tblCompBranchHistory, and build a paramaterised query to
update locationmasterhistory set export = "yes" where ID in [list]
but I'm not 100% sure of your schema

btw, I suggest updating by 'ID', because in as little info as you've shown, doing an unrestricted update of locationmasterhistory could be dangerous
Member 12611727 4-Aug-20 0:40am    
Hi, But i dont want to update the table, I want to update the excel column export = "yes". Before or after export to excel, I want to see the excel column as Yes.
DerekT-P 4-Aug-20 9:54am    
so it's got nothing to do with the select statement then; you just want a column that says "yes"... ? So after the "column" for..next, xlWorkSheet.Cells[i + 2, dtable.Columns.Count] = "Yes"; (assuming the yes/no column is immediately to the right of the data column). BTW, you're saving the entire workbook after updating each cell; fine if there's a handful of rows being returned, but probably not so great if there's a few tens of thousands...! We may have misunderstood you, but you've not explained what you're trying to do all that clearly.

1 solution

I work Heavily with SQL Server Reporting Services(SSRS), and the relaying of DATA to an excel sheet is done for you; if it is just the DB Data you want to relay. So if your client just wants the DATA with some conditional data amended to the excel sheet look to utilizing SSRS.

To Note SSRS is a paid for service by MSFT. In SSRS you can add excel type logic to the results from the data.


What is SQL Server Reporting Services - SQL Server Reporting Services (SSRS) | Microsoft Docs[^]



This link will run you through the basics of SSRS. I am not an MSFT employee or sales rep just speaking from experience.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900