none
Windows Application to Windows Service RRS feed

  • Question

  • Hi all,

     

    I have a Windows Application that connects to an OPC Server and saves values to a Sql Server Database using different Forms.Timers.

    This works fine and i wanted to transform it into a Windows Service. I've read much about creating Windows Service and so i started a new Visual Studio project and started copying my code. Only thing i had to change was the type of my Timers (now i'm using System.Timers.Timer).

    My problems start when i run the service (while debbuging or after install). My main procedure does lots of selects to my database and i save the results inside datasets. To try and make it simple to explain my issue imagine i have 3 datasets with data on them... after a while the data that should be on the first dataset is instead on the second one, and the data of the second is on the third one. So when i try to access a column from my first dataset i get an exception saying that the column doesn't exist.

     

    As anybody ever experienced something like this? I'm on a dead end and i would really appreciate any help please.

     

    Best regards.

    • Moved by SamAgain Friday, June 18, 2010 7:14 AM Possible DataSet Related (From:Common Language Runtime)
    Thursday, June 17, 2010 7:05 PM

All replies

  • Hi,

    This is not clear to me how it could happen. From your explanations it looks like all three datasets has at least one common column, correct? It could help to understand problem if you'll be more specific. So for now my guesses:

     The only reason you could meet this issue is configuration issue, in case if your code is correct. Could you check dataset's columns and make sure it's in place. After if everything is OK, please check spelling and all stuff related to typos in commands etc.. At the end if everythig is correct could you post full exception and inner exception if applicable + please tell more about your datasets.


    Regards
    Thursday, June 17, 2010 7:54 PM
  • Hi,

     Thanks for your reply. I will post part of the procedure that 2 of my timers invoke at certain intervals:

     

    //Dataset and DataAdapter declaration
    //When i declare them as procedure variables the problem still occurs 
      private static DataSet dsGamas = new DataSet();
      private static DataSet ds = new DataSet();
      private static DataSet dsFormulasParametros = new DataSet();
    
      private static SqlDataAdapter drGamas = new SqlDataAdapter();
      private static SqlDataAdapter daAdapter = new SqlDataAdapter();
      private static SqlDataAdapter drFormulasParametros = new SqlDataAdapter();
    
      private static string queryGamas = "";
      private static string queryString = "";
      private static string queryFormulasParametros = "";
    
    //Timers procedure
    public void validacaoValores(string sender)
    {
      try
      {
         Monitor.Enter(lockObj);
    
         try
        {
    
        for (int i = 0; i <= OPCIsInst.Count - 1; i++)
        {
    
        try
        {
        queryGamas = "";
        queryString = "";
        queryFormulasParametros = "";
    
        dsGamas.Clear();
        dsGamas.Reset();
    
        ds.Clear();
        ds.Reset();
    
        dsFormulasParametros.Clear();
        dsFormulasParametros.Reset();
    
        string queryGamas = "SELECT TOP 1 [m_value], [b_value] FROM [DB].[dbo].[Gamas] WHERE([ID_parametro_FK] = " + startRow["id_parametro_PK"] + ") ORDER BY [data_ini] DESC";
        lastSQLCmm = queryGamas;
        drGamas.SelectCommand = new SqlCommand(queryGamas, connection);
        drGamas.SelectCommand.CommandTimeout = 15;
    
        drGamas.Fill(dsGamas); // This dataset has 2 columns
    
        string queryString = "SELECT * FROM [DB].[dbo].[Instant_AUX] WHERE [id_parametro_FK] = " + startRow["id_parametro_PK"];
        lastSQLCmm = queryString;
        daAdapter.SelectCommand = new SqlCommand(queryString, connection);
        daAdapter.SelectCommand.CommandTimeout = 15;
    
        daAdapter.Fill(ds); //This dataset has 5 columns
    
        string queryFormulasParametros = "SELECT * FROM [DB].[dbo].[tbl_rel_param] ORDER BY [prioridade] ASC";
    
         lastSQLCmm = queryFormulasParametros;
         drFormulasParametros.SelectCommand = new SqlCommand(queryFormulasParametros, connection);       
         drFormulasParametros.SelectCommand.CommandTimeout = 15;
    
         drFormulasParametros.Fill(dsFormulasParametros); //This dataset has 6 columns
    
         //The error happens here. I catch the exception and the procedure loops to the next item
    
         //the rest of the code goes here with more SQL Selects and Inserts
         ....
    
         queryGamas = "";
         queryString = "";
         queryFormulasParametros = "";
    
         dsGamas.Clear();
         dsGamas.Reset();
    
         ds.Clear();
         ds.Reset();
    
         dsFormulasParametros.Clear();
         dsFormulasParametros.Reset();
    
         GC.Collect();
    
        }
        catch (Exception ex)
        {
         Service.createLogFileError(ex.Message, " validacaoValores");
    
        }
        finally
        {
         Monitor.Exit(lockObj);
    
        }
      }
    

     All datasets have different columns and what happens is that, after a while, i start getting exceptions saying that "column X does not belong to table Y".  I catch the exception and the procedure loops to the next item.

    After the error i debbug my datasets and i see that the first one (dsGamas) has the columns and the rows of the previous third one (dsFormulasParametros, from the previous loop), the second one has the columns and rows of the first one (from the current loop) and that the third one has the data from the second one. Then the error happens again.

    I cant understand what is happening. As you can see i am clearing all the data from the datasets, query strings and data adapters. As i mentioned before it all goes well when its a normal Windows Application, this only occurs when i try to build a Windows Service.

    Any help is very welcome. Hope i have made myself clearer this time.

    Best regards

     

    Friday, June 18, 2010 9:33 AM