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:
i do small program like invoice i do all thing good except
auto sequence number
i want use label (number) and button (export)
to finish from this invoice
then go to next automatically

int num = 0;
num++;
label.Text = num.ToString();

the problem is
when i close program and open it again
the sequence go start from (1)

is there method for save on txt file and make the program get last digit saved
then add (1) on last digit ?
by StreamWriter & StreamReader or others ???

What I have tried:

int num = 0;
StreamWriter sw = new StreamWriter("Data_ID.txt", true);
string sw_AddID = lblID.Text ;
sw.WriteLine(sw_AddID);
num++;
sw.Close();

lblID.Text = num.ToString();
Posted
Updated 18-Mar-20 17:58pm
Comments
gggustafson 18-Mar-20 13:53pm    
You have a number of problems. I suggest that you write down (on a piece of paper) what the sequence of actions are. I'd say that the first is to test of Data_ID.txt exists; if it does then read the value of num from the file and close the file; otherwise set num to its first value minus one. Do what you need to do with num++. Write the new value of num to Data_ID.txt. NOTE: in your code, you didn't read the value so num is always save as 1.
ZaYeD1 18-Mar-20 17:27pm    
look again
when i close the program and open it again
i want read first last digit in ID then add +1 for get new ID
example if i do ID 1 , 2 and 3 that mean 3x invoice but if i close program
and i open it again the ID start from 1
and i want start from 4
how i do that???

i do this only

int num = 0;

private void btnID_Click(object sender, EventArgs e)


 StreamWriter sw = new StreamWriter("Data_ID.txt", true);
                string sw_AddID = lblID.Text + ";";
                sw.WriteLine(sw_AddID);
                num++;
                sw.Close();

                lblID.Text = num.ToString();
gggustafson 18-Mar-20 20:27pm    
I think we are having a language misunderstanding. You say that you want to read but nowhere in your code do you read. You need to use a StreamReader to read the file and a StreamWriter to write the file. As I said in my original response, you are not performing the read.

1 solution

C#
/// In the original example, a relative filename was used. This is not 
/// what should be used. The directory name of a relative path will 
/// default to the directory containing the executable that is 
/// currently executing. Even if that is desired, and it is normally 
/// not desired,the full pathname should be provided, using code like
/// 
///     filename = Path.GetDirectoryName ( 
///                         Assembly.
///                         GetEntryAssembly ( ).
///                         Location ) + 
///                <relative-filename>;
/// 
/// By so doing, following programmers will more easily understand the 
/// location of the file.
/// 
/// It is not recommended that a data file be collocated with the 
/// executable because there is no connection between the two. The 
/// executable could be moved, thereby invalidating the pathname to 
/// the data file.
/// 
/// If the file is located in some other location, its fully qualified 
/// pathname should be provided.
/// 
/// When using StreamReader and StreamWriter, enclose reading and 
/// writing within a using block. This insures that the streams will 
/// be closed (like enclosing the block in a try-finally block without 
/// the hassle of a finally block).
/// 
/// The following is an example using these thoughts.
using System;
using System.IO;
using System.Reflection;

class demonstration
    {
    int     data_id = 0;
    string  filename = Path.GetDirectoryName ( 
                                Assembly.
                                GetEntryAssembly ( ).
                                Location ) + 
                       "Data_ID.txt";

    // *************************************************** demonstrate
    
    void demonstrate ( )
        {
// *************************************** AT THE START OF THE PROGRAM
                                        
                                        // if the file exists, read 
                                        // the value of data_id; 
                                        // otherwise leave its value 
                                        // as zero since it is about 
                                        // to be incremented
        if ( File.Exists ( filename ) )
            {
            try 
                {
                using ( StreamReader sr = 
                                     new StreamReader ( filename ) )
                    {
                    string  line = sr.ReadLine ( );
                    
                    if ( !String.IsNullOrEmpty ( line ) )
                        {
                        data_id = Convert.ToInt32 ( line );
                        }
                    }
                }
            catch 
                {
                
                }
            }
        data_id++;

// ********************************** PERFORM SOME ACTION WITH data_id

// ***************************************** AT THE END OF THE PROGRAM

                                        // note that by setting the 
                                        // second parameter false, 
                                        // the data is not appended,
                                        // rather the file is over-
                                        // written
        try 
            {
            using ( StreamWriter sw = new StreamWriter ( filename, 
                                                         false ) )
                {
                sw.WriteLine ( data_id );
                }
            }
        catch 
            {
            
            }

        } // demonstrate

    } // class demonstration
 
Share this answer
 
Comments
ZaYeD1 19-Mar-20 20:45pm    
thank you very much :)

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