Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to avoid repeated output statement...

How to avoid repeated output statement...

Scheduled Pinned Locked Moved C#
helpdatabasexmltutorial
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Calla

    One simple approach could be to create a bool variable called isPrinted and set it to false before your looping begins. When you reach a statement that will print a message saying that the file doesn't exist - check if the isPrinted is true. If not, then print your message and set isPrinted = true. The next time you reach the print statement your message will not print. I didn't read your code very thorough, but it seems to me that you might want to change your design if you run into these kind of problems. Take a minute to think about it. Good luck! :)

    T Offline
    T Offline
    Tash18
    wrote on last edited by
    #3

    thanx for ur reply... but that boolean function wont work in my condition because i have more that 5000 files and about 300 files are missing.. Ur case wil work if i have only one file which is missing... i think u got my point... Regards, Tash

    1 Reply Last reply
    0
    • T Tash18

      Hi guyz.. Im still on my project as the final touch up i have a task to be added to my project.. As i have mentioned in my previous post my project is on console application.. Thank you 2 all in code project because from the help and advice that i got from everyone at code project i was able 2 completed 90% of my project.. now the issue in my project it that my project migrates all the records stored in xml to the database its fine with it, certain records in the xml file are stored in another file in xml my application catches the file exactly and inserts the records from it. now i have a verification coded in my application which checks whether that particular file is present in the folder, if the file is present the records from it are migrated if the file is not present, on the output window it displays the name of the file stating that the file is missing in the folder.. now since my application has nested for loops and if condition the verification for the same file takes place more than once so in the output window the same missing file name is printed more than one(may be 10times) it becomes messy 2 see the output window.. Now what i would try to implement is that even if it checks the same file for the second time and if the file is not present it should display the message on the output window only one.. my coding is as follows:

      class Class1
      {

      	XmlDataDocument doc1 = new XmlDataDocument();
      	doc1.Load(ConfigurationSettings.AppSettings\["fun"\]);
      	XmlNodeList nodes1 = doc1.GetElementsByTagName("Row");
      	foreach(XmlNode node1 in nodes1)
      	{
      		if(node1.HasChildNodes && node1.ChildNodes.Count > 0)
      		{
      			XmlElement Element1 = (XmlElement) node1;
      			string f\_ID = Element1.GetElementsByTagName("Id")\[0\].InnerText;
      			string f\_AppId = Element1.GetElementsByTagName("AppId")\[0\].InnerText;
      			string f\_FileName = Element1.GetElementsByTagName("FileName")\[0\].InnerText;
      			if(f\_AppId==app)
      			{
      				string path = ConfigurationSettings.AppSettings\["funfile"\]+"\\\\"+f\_FileName+".xml";
      				if (File.Exists(path))
      				{
      					XmlDataDocument doc2 = new XmlDataDocument();
      					doc2.Load(ConfigurationSettings.AppSettings\["funfile"\]+"\\\\"+f\_FileName+".xml");
      					XmlNodeList nodes2 = doc2.GetElementsByTagName("Function");
      					foreach(XmlNode node2 in nodes2)
      					{
      						if(node2.HasChildNodes && node2.ChildNodes.Count > 0)
      						{
      							XmlElement Element2 = (XmlElement) node2;
      							string ff\_Id = Element2.GetElementsByTagName("Id")\[0\].InnerTex
      
      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #4

      Hello, You could hold your outputed filenames in a list and make a lookup in the list bevor you do the next output. Like this (using a generic List):

      List<string> fileNames = new List<string>();

      string f_FileName = ...;
      ...
      // foreach ...
      if (!fileNames.Contains(f_FileName))
      {
      fileNames.Add(f_FileName);
      Console.WriteLine("Please check Function file-{0} fields do not match", f_FileName);
      }

      Hope it helps!

      All the best, Martin

      T 1 Reply Last reply
      0
      • M Martin 0

        Hello, You could hold your outputed filenames in a list and make a lookup in the list bevor you do the next output. Like this (using a generic List):

        List<string> fileNames = new List<string>();

        string f_FileName = ...;
        ...
        // foreach ...
        if (!fileNames.Contains(f_FileName))
        {
        fileNames.Add(f_FileName);
        Console.WriteLine("Please check Function file-{0} fields do not match", f_FileName);
        }

        Hope it helps!

        All the best, Martin

        T Offline
        T Offline
        Tash18
        wrote on last edited by
        #5

        thanx but genericlist can be used in Dotnet framework 2.0 but im using VS2003 on framework 1.0 :(( .. can i make use of array.. but the problem is i am not very much strong in arrays.. Do please guide me... Thanx in advance.. Regards, Tash

        M 1 Reply Last reply
        0
        • T Tash18

          thanx but genericlist can be used in Dotnet framework 2.0 but im using VS2003 on framework 1.0 :(( .. can i make use of array.. but the problem is i am not very much strong in arrays.. Do please guide me... Thanx in advance.. Regards, Tash

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #6

          Hello,

          <> wrote:

          thanx but genericlist can be used in Dotnet framework 2.0 but im using VS2003 on framework 1.0

          that's true!

          <> wrote:

          can i make use of array.. but the problem is i am not very much strong in arrays.. Do please guide me.

          Sure! instead of generics, you could use the StringCollection class, which is available in .Net 1.x:

          using System.Collections.Specialized;

          ...
          StringCollection fileNames = new StringCollection();

          StringCollection, does also provide a Contains and a Add method, like the generic list. Hope it helps!

          All the best, Martin

          T 1 Reply Last reply
          0
          • M Martin 0

            Hello,

            <> wrote:

            thanx but genericlist can be used in Dotnet framework 2.0 but im using VS2003 on framework 1.0

            that's true!

            <> wrote:

            can i make use of array.. but the problem is i am not very much strong in arrays.. Do please guide me.

            Sure! instead of generics, you could use the StringCollection class, which is available in .Net 1.x:

            using System.Collections.Specialized;

            ...
            StringCollection fileNames = new StringCollection();

            StringCollection, does also provide a Contains and a Add method, like the generic list. Hope it helps!

            All the best, Martin

            T Offline
            T Offline
            Tash18
            wrote on last edited by
            #7

            Hurray!!! it worked.... Thank u.. Million thanx.. Now my application is fully finished one.. once again thank you buddy... Regards, Tash

            M 1 Reply Last reply
            0
            • T Tash18

              Hurray!!! it worked.... Thank u.. Million thanx.. Now my application is fully finished one.. once again thank you buddy... Regards, Tash

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #8

              <> wrote:

              Hurray!!! it worked.... Thank u.. Million thanx.. Now my application is fully finished one.. once again thank you

              Wow, I'm really glad I could help!

              All the best, Martin

              1 Reply Last reply
              0
              • T Tash18

                Hi guyz.. Im still on my project as the final touch up i have a task to be added to my project.. As i have mentioned in my previous post my project is on console application.. Thank you 2 all in code project because from the help and advice that i got from everyone at code project i was able 2 completed 90% of my project.. now the issue in my project it that my project migrates all the records stored in xml to the database its fine with it, certain records in the xml file are stored in another file in xml my application catches the file exactly and inserts the records from it. now i have a verification coded in my application which checks whether that particular file is present in the folder, if the file is present the records from it are migrated if the file is not present, on the output window it displays the name of the file stating that the file is missing in the folder.. now since my application has nested for loops and if condition the verification for the same file takes place more than once so in the output window the same missing file name is printed more than one(may be 10times) it becomes messy 2 see the output window.. Now what i would try to implement is that even if it checks the same file for the second time and if the file is not present it should display the message on the output window only one.. my coding is as follows:

                class Class1
                {

                	XmlDataDocument doc1 = new XmlDataDocument();
                	doc1.Load(ConfigurationSettings.AppSettings\["fun"\]);
                	XmlNodeList nodes1 = doc1.GetElementsByTagName("Row");
                	foreach(XmlNode node1 in nodes1)
                	{
                		if(node1.HasChildNodes && node1.ChildNodes.Count > 0)
                		{
                			XmlElement Element1 = (XmlElement) node1;
                			string f\_ID = Element1.GetElementsByTagName("Id")\[0\].InnerText;
                			string f\_AppId = Element1.GetElementsByTagName("AppId")\[0\].InnerText;
                			string f\_FileName = Element1.GetElementsByTagName("FileName")\[0\].InnerText;
                			if(f\_AppId==app)
                			{
                				string path = ConfigurationSettings.AppSettings\["funfile"\]+"\\\\"+f\_FileName+".xml";
                				if (File.Exists(path))
                				{
                					XmlDataDocument doc2 = new XmlDataDocument();
                					doc2.Load(ConfigurationSettings.AppSettings\["funfile"\]+"\\\\"+f\_FileName+".xml");
                					XmlNodeList nodes2 = doc2.GetElementsByTagName("Function");
                					foreach(XmlNode node2 in nodes2)
                					{
                						if(node2.HasChildNodes && node2.ChildNodes.Count > 0)
                						{
                							XmlElement Element2 = (XmlElement) node2;
                							string ff\_Id = Element2.GetElementsByTagName("Id")\[0\].InnerTex
                
                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #9

                Hello again! Apart from the solution I provided, I would like you to know, that you should take care about the ressources used by the OdbcCommand instances.

                <> wrote:

                cmd = new OdbcCommand...

                Here you are instanciating allways a new OdbcCommand in a nested loop, and do not care about the ressources. The OdbcCommand[^] derives from System.ComponentModel.Component[^], which does not implement IDisposable but provides a Dispose[^] method, you should call (cmd.Dispose();).

                All the best, Martin

                T 2 Replies Last reply
                0
                • M Martin 0

                  Hello again! Apart from the solution I provided, I would like you to know, that you should take care about the ressources used by the OdbcCommand instances.

                  <> wrote:

                  cmd = new OdbcCommand...

                  Here you are instanciating allways a new OdbcCommand in a nested loop, and do not care about the ressources. The OdbcCommand[^] derives from System.ComponentModel.Component[^], which does not implement IDisposable but provides a Dispose[^] method, you should call (cmd.Dispose();).

                  All the best, Martin

                  T Offline
                  T Offline
                  Tash18
                  wrote on last edited by
                  #10

                  Thanx for ur reply... as i have already mentioned my application is too big to post the complete code over here....

                  Martin# wrote:

                  you should call (cmd.Dispose();).

                  i have a finally block which is as follows:

                  		finally
                  		{
                  			if (rdr != null)
                  			{
                  				rdr.Close();
                  			}
                  			if (con != null)
                  			{
                  				con.Close();
                  				con.Dispose();
                  			}
                                              cmd.Dispose
                  		}
                  

                  Thanx for ur advice i really appreciate it... Regards, Tash

                  1 Reply Last reply
                  0
                  • M Martin 0

                    Hello again! Apart from the solution I provided, I would like you to know, that you should take care about the ressources used by the OdbcCommand instances.

                    <> wrote:

                    cmd = new OdbcCommand...

                    Here you are instanciating allways a new OdbcCommand in a nested loop, and do not care about the ressources. The OdbcCommand[^] derives from System.ComponentModel.Component[^], which does not implement IDisposable but provides a Dispose[^] method, you should call (cmd.Dispose();).

                    All the best, Martin

                    T Offline
                    T Offline
                    Tash18
                    wrote on last edited by
                    #11

                    sorry sorry i thought u were speaking abt the connection obj... okay fine i will take up ur advice of using cmd.Dispose(); Thanx alot.. Regards, Tash

                    M 1 Reply Last reply
                    0
                    • T Tash18

                      sorry sorry i thought u were speaking abt the connection obj... okay fine i will take up ur advice of using cmd.Dispose(); Thanx alot.. Regards, Tash

                      M Offline
                      M Offline
                      Martin 0
                      wrote on last edited by
                      #12

                      <> wrote:

                      okay fine i will take up ur advice of using cmd.Dispose();

                      :thumbsup:

                      All the best, Martin

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups