Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all,

I wrote the code for converting a database table into a notepad and its working fine, but my problem is that when a user makes an update in the table (if he changed any fields in table like firstname or lastname or anything) it needs to be updated in the notepad also.
The code I wrote for conversion to notepad is:
C#
  private void Form1_Load(object sender, EventArgs e)
        { 
          Timer MyTimer = new Timer();       
          MyTimer.Interval = (10000); 
          MyTimer.Tick += new EventHandler(timer1_Tick_1);      
          MyTimer.Start();         
        }

 private void timer1_Tick_1(object sender, EventArgs e)
        {
            employeedetails();
        }

public void employeedetails()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["nConnectionString"].ConnectionString);

            // using (SqlConnection connection = new SqlConnection(str))
            {
                try
                {
                    conn.Open();
                }
                catch (System.Data.SqlClient.SqlException ex)
                {

                    return;
                }
                string selectCommandText = "SELECT distinct      userid,fullname,lastname,firstname,localjobtitle,employmentstatusid,dateofjoining,dateofexit,siteid,email,topfunctionid,active,exitingthecompany,lmsroleid,locallanguagejobtitle FROM tblUserProfile up,tblreportingto tr,tblCountry where temo is null ";
                using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, conn))
                {
                    using (DataTable table = new DataTable("tblUserProfile"))
                    {
                        adapter.Fill(table);
                        StringBuilder commaDelimitedText = new StringBuilder();
                        //commaDelimitedText.Append("EMP_Number|Name|Lastname|Firstname|Middleinitial|NameSuffix|Positiontitle|EMPcode|EMPstatus|Manager number|Approvernumber|Startdate|Enddate|Lastreviewdate|Address1|Address2|City|State|Zipcode|Country|Email|Phone1|Phone2|Fax|URL|Note|DomainCode|Organizationcode|Jobcode|Active|Enabled|Username|NTlogin|Role|Timezone|Currency|Language|Text1|Text2|Text3|Text4|Memo1|Date1|Date2|Indicator1|Money1|Integer1|Float1|JobJoiningdate|Organizationjoiningdate");
                        foreach (DataRow row in table.Rows)
                        {
                            string value = string.Format("{0}|{1}|{2}|||{3}||{4}|{5}||||||{6}||{7}|{8}|{9}||||||||{10}|{11}|{12}|{13}|", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]);
                            commaDelimitedText.AppendLine(value);
                        }

                        File.AppendAllText(@"C:\Documents and Settings\aj99823\Desktop\project\Employee Details.txt", commaDelimitedText.ToString());

                    } 
                }
            }
        }

I am a new comer to programming.

I thought the reason is since I am using a timer, in every interval it will append the text to notepad. So repeated data are coming to the notepad.

Can anyone help me.
Posted
Updated 28-Feb-12 19:23pm
v2
Comments
André Kraak 29-Feb-12 1:23am    
Edited question:
Added pre tags
Spelling/Grammar

1 solution

The code provided above will append records to the file indefinitely no matter the state of the data. AppendAllText will keep appending the same data to the file. The type of file created in the code provided seems to indicate it is meant to be delimiter separated value file. These are generally meant to be one-way operations of Read/Write (note the exclusion of Update and Insert). A few suggestions:

1. The code provided is using data from the DataTable to update the file. Therefore, make sure there is code to update the DataTable when a user makes a change, otherwise the new data will never make it to the file. The database, specifically, will need to be updated to include the user changes.

2. Consider recreating the file as apposed to appending. The code provided will keep appending all of the data in the DataTable upon each timer iteration generating duplicate data in the file. By creating/opening the file, overwriting the text inside it, and closing the file will prevent the duplicate data issue. To do this a combination of File methods will be needed: OpenWrite and WriteAllText

3. Assuming scalability was in mind, parsing the file will be needed. The idea is to open the file, look for the particular record, update the record, and then save/close the file. To parse the file there must be a way to uniquely identify the record to update. At this point, a text file is beginning to be more unsuitable from a maintenance stand point. There are libraries available to manage CSV files such as LINQ-to-CSV Library - Code Project and FileHelpers.

4. Use an XML file instead. The .NET framework has great support for XML file management. More information about this can be found on MSDN. Please reference XMLDocument, DataSet and XmlDataDocument Synchronization, and Create, Modify and Read XML Files in C#.
 
Share this answer
 
Comments
amaljosep 30-Mar-12 8:17am    
thanks a lot for your help........

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