Share via


Program works on Visual Studio but not works as ".exe"

Question

Wednesday, November 10, 2010 12:25 PM

Hello,
Im just working on a program that changes some row's values in an excel file. Program has just about 60 rows. When i debug my program at Visual Studio (pressing F5), its working properly and made some changes at excel file. Its ok. But when I only click my project's "excel.exe" and run it manually, it doesnt work properly. Its getting no error but it didnt do any changes at this excel file. And i didnt move this "excel.exe" file to anywhere, i started it in projects folder.(/bin/debug)

 Its really weird and i didnt know what is the problem?

My all codes below;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace badi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }I

private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dosya.xlsx;Extended Properties=Excel 12.0 Xml;");
            baglanti.Open();
            string query = "SELECT [Ci Adi],[Baslangic Tarihi],[Bitis Tarihi],[Sira],[Kategori] FROM [Sheet6$A:L]";

            OleDbDataReader rdr;
            OleDbCommand komut = new OleDbCommand(query, baglanti);
            rdr = komut.ExecuteReader();

            while (rdr.Read())
            {
                DateTime tarih4_baslangic = DateTime.Parse(rdr["Baslangic Tarihi"].ToString());
                DateTime tarih4_bitis = DateTime.Parse(rdr["Bitis Tarihi"].ToString());

                string ci_adi = rdr["Ci Adi"].ToString();
                string query_ci = "SELECT [Ci Adi],[Baslangic Tarihi],[Bitis Tarihi],[Sira],[Kategori] FROM[Sheet6$A:L] WHERE [Ci Adi]='" + ci_adi + "'";
                OleDbDataReader rdr_ci;
                OleDbCommand komut_ci = new OleDbCommand(query_ci, baglanti);

                rdr_ci = komut_ci.ExecuteReader();
               
                while (rdr_ci.Read())
                {
                    string birincidongu = rdr_ci["Sira"].ToString();
                    string ikincidongu = rdr["Sira"].ToString();
                       
                    DateTime tarih4_baslangic = DateTime.Parse(rdr_ci["Baslangic Tarihi"].ToString());
                    DateTime tarih4_bitis = DateTime.Parse(rdr_ci["Bitis Tarihi"].ToString());

                    if (int.Parse(rdr_ci["Sira"].ToString()) != int.Parse(rdr["Sira"].ToString()))
                    {
                        if (int.Parse(rdr_ci["Sira"].ToString()) > int.Parse(rdr["Sira"].ToString()))
                        {
                            if (tarih4_baslangic > tarih4_baslangic && tarih4_bitis > tarih4_baslangic)
                            {
    
                                OleDbCommand komut_ustune_yaz = new OleDbCommand("UPDATE [Sheet6$] SET [Kategori]='OK' WHERE [Sira]='" + rdr["Sira"] + "'  ", baglanti);
                                komut_ustune_yaz.ExecuteNonQuery();
                            }
                            else if (tarih4_baslangic >= tarih4_baslangic && tarih4_baslangic <= tarih4_bitis)
                            {
                                if (rdr["Kategori"].ToString() == "OK")
                                {

                                }
                                else
                                {
                                    OleDbCommand komut_ustune_yaz = new OleDbCommand("UPDATE [Sheet6$] SET [Kategori]='OK' WHERE [Sira]='" + rdr_ci["Sira"] + "'  ", baglanti);
                                    komut_ustune_yaz.ExecuteNonQuery();
                                }
                            }
                        }
                    }
                }
                
                rdr_ci.Close();
 
            }
         
        }

All replies (12)

Thursday, November 11, 2010 8:27 PM âś…Answered

I would try to put a breakpoint at the first executable line of the button click method, so to block as soon as possible the execution when in the form you click the button, and then step through via F10/F11. If the code is not hit by the debugger, then try recompiling again, because it could be due to have a different executable in respect to the source code, and they must match to attach successfully a process. To be absolutely sure that you point to the correct folder, delete the bin folder and upon recompilation VS will regenerate it. Let me know, looks interesting. P.S. I also suggest to put some messagebox in the middle of the code to help you follow better what's up.

Giuseppe

Hello again,

I compile again and put breakpoint in the "button_click" and I attach it to proceess and it stop at this breakpoint. Actually I didnt understand what i did wrong because I did everyhing same before.. But anyway, now I can debug the .exe file.

I noticed another thing. My excel file has 7500 rows. When I double click and run the .exe file and click the button, it didnt change any row at the excel file. After that I erase about 6500 rows and saved it to excel file. I tried again, run the .exe file, and it works normally! "UPDATE" query works and changes the some rows. This is weird. 

Then I double click the .exe file again and I attached it to process from VS and I use F11, I noticed that codes are going normally. I mean If a row must be change, "UPDATE" query's row is working. But it didnt change any row at the excel file, when excel file has approximately 1000+ rows.

Finally, when I press "F5" at VS, it changes rows, "UPDATE" query works normally, even there are 7500 rows has.

Same .exe file works different from VS and from double clicking.

Finally I solve my problem but in another way. I save my excel file from .xlsx to .xls. I change format of the file to excel 2003 and now its ok. But the problem is above interesting too.


Wednesday, November 10, 2010 1:22 PM

Try using process monitor to see whether it attempts to access the file.HTH Ciaran http://wannabedeveloper.spaces.live.com


Wednesday, November 10, 2010 1:27 PM

Maybe you changed the configuration to Release. In which case, the right exe would be in bin\Release.


Wednesday, November 10, 2010 1:56 PM

Actually I dont know how changing to release. My all files in "/bin/debug" folder.

 

Ciaran, I think there is no problem at relation with excel file. Because if the .exe file couldnt connect the excel file, it will be geting error.   .exe is seeing excel file but didnt do anything when I run manually.


Wednesday, November 10, 2010 3:15 PM

I suggest you to launch the exe by doubleclicking on its name in the debug folder, like you already did. Then, from Visual Studio, choose "Debug -> Attach to process" and choose your program from the list. Now you are debugging the real process as run into Windows. Put in your code in VS some breakpoints and then launch the operation clicking on your button. So you can debug the executing process and see what really happens.

Giuseppe


Wednesday, November 10, 2010 4:25 PM

hi,

when u use any database like excel DB , when you run application on Visual studio, at first time visual studio copy your excel DB to BIN->Debug->Data directory

 

when you run your .exe file application will be run but

 all your changes will be done on BIN->Debug->Data->yourExcelDB not on your main  ExcelDB File.

 

 

va-ansari


Wednesday, November 10, 2010 9:27 PM

Giuseppe, thanks for your comments. I run the .exe file and then I "Attach to process" like you said before. Im adding breakpoints but the program wont stop at breakpoints noway. I add breakpoint to another rows but it didnt catch. How can i do that?


Wednesday, November 10, 2010 9:32 PM

Vali, thanks for your reply. I tryed your suggestions but it didnt work out. Firstly there is no folder like "bin/debug/data".. Only "bin/debug/" folder..

I copyed excel file to .exe file's folder. I mean .exe is already sees to excel file. Secondly I tryed to "release" mode debug but it didnt work too.


Thursday, November 11, 2010 9:03 AM

I would try to put a breakpoint at the first executable line of the button click method, so to block as soon as possible the execution when in the form you click the button, and then step through via F10/F11. If the code is not hit by the debugger, then try recompiling again, because it could be due to have a different executable in respect to the source code, and they must match to attach successfully a process. To be absolutely sure that you point to the correct folder, delete the bin folder and upon recompilation VS will regenerate it. Let me know, looks interesting. P.S. I also suggest to put some messagebox in the middle of the code to help you follow better what's up.

Giuseppe


Thursday, November 11, 2010 1:51 PM

I would try to put a breakpoint at the first executable line of the button click method, so to block as soon as possible the execution when in the form you click the button, and then step through via F10/F11. If the code is not hit by the debugger, then try recompiling again, because it could be due to have a different executable in respect to the source code, and they must match to attach successfully a process. To be absolutely sure that you point to the correct folder, delete the bin folder and upon recompilation VS will regenerate it. Let me know, looks interesting. P.S. I also suggest to put some messagebox in the middle of the code to help you follow better what's up.

Giuseppe

Hello again,

I compile again and put breakpoint in the "button_click" and I attach it to proceess and it stop at this breakpoint. Actually I didnt understand what i did wrong because I did everyhing same before.. But anyway, now I can debug the .exe file.

I noticed another thing. My excel file has 7500 rows. When I double click and run the .exe file and click the button, it didnt change any row at the excel file. After that I erase about 6500 rows and saved it to excel file. I tried again, run the .exe file, and it works normally! "UPDATE" query works and changes the some rows. This is weird. 

Then I double click the .exe file again and I attached it to process from VS and I use F11, I noticed that codes are going normally. I mean If a row must be change, "UPDATE" query's row is working. But it didnt change any row at the excel file, when excel file has approximately 1000+ rows.

Finally, when I press "F5" at VS, it changes rows, "UPDATE" query works normally, even there are 7500 rows has.

Same .exe file works different from VS and from double clicking.


Tuesday, February 22, 2011 4:50 PM

 I have implemented SSIS objects programmatically in C#.

The applicatin gives correct output when I am running the application from Visual studio(VShost.exe) but when I run the .exe from debug folder it gives incorrect output.

Is there any difference in behaviour of both the exes or is there any settings need to be done.


Wednesday, February 23, 2011 7:09 AM

Hi

I have created an appliation which generates SSIS packages(.dtxs). I have implemented UnionAll component which is supposed to return 30 rows.

But when I execute my application using .exe from bin folder it returns only 10 rows. When I execute my application from VS(VSHost.exe) it works fine and returns 30 rows.

Please let me know if you any idea for the same.

Thanks