Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm using SQLITE Database. I want to create a custom invoice number like this "INNK-5001" with auto increment INNK-5002, INNK-5003... and so on. I write below code for this purpose. Below code working correctly only if i use only number like "5000" but when i use "INNK-5000" i got Error. Please help me how can i get this number "INNK-5000" with auto increment using Sqlite Database. This my code...

What I have tried:

// To Create Sqlite Database

C#
public void CreateDB()
    {
        if (!File.Exists("customid.db"))
        {
            SQLiteConnection.CreateFile("customid.db");
            using (SQLiteConnection conn = new SQLiteConnection("Data Source=customid.db;Version=3;"))
            {
                string commandstring = "CREATE TABLE cusid (Id INTEGER PRIMARY KEY NOT NULL, FirstName NVARCHAR(250), LastName NVARCHAR(250))";
                using (SQLiteCommand cmd = new SQLiteCommand(commandstring, conn))
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }


// For Custom Invoice Number

C#
public void CustomId2()
    {
        try
        {
            using (SQLiteConnection conn = new SQLiteConnection("Data Source=customid.db;Version=3;"))
            {
                string CommandText = "SELECT Id FROM cusid";
                using (SQLiteDataAdapter sda = new SQLiteDataAdapter(CommandText, conn))
                {
                    conn.Open();
                    DataTable datat = new DataTable();
                    sda.Fill(datat);

                    if (datat.Rows.Count < 1)
                    {
                        textBox1.Text = "INNK-5000";
                    }
                    else
                    {
                        using (SQLiteConnection conn1 = new SQLiteConnection("Data Source=customid.db;Version=3;"))
                        {
                            string CommandString = "SELECT MAX(Id) FROM cusid";
                            using (SQLiteCommand cmd = new SQLiteCommand(CommandString, conn))
                            {
                                conn1.Open();
                                int a = Convert.ToInt32(cmd.ExecuteScalar());
                                a = a + 1;
                                textBox1.Text = a.ToString();
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


// For Save a Record into Database

C#
private void savebtn_Click(object sender, EventArgs e)
    {
        try
        {
            using (SQLiteConnection conn = new SQLiteConnection("Data Source=customid.db;Version=3;"))
            {
                string CommandText = "INSERT INTO cusid ([Id], [FirstName], [LastName]) VALUES (@id,@firstname,@lastname)";
                using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
                {
                    cmd.Parameters.AddWithValue("@id", textBox1.Text);
                    cmd.Parameters.AddWithValue("@firstname", textBox2.Text);
                    cmd.Parameters.AddWithValue("@lastname", textBox3.Text);

                    conn.Open();
                    int a = cmd.ExecuteNonQuery();
                    if (a > 0)
                    {
                        MessageBox.Show("Data Saved!!");
                        CustomId2();
                        BindDataGridView();
                    }
                    else
                    {
                        MessageBox.Show("Not Saved!!");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }



Please Help me... Thank You.
Posted
Updated 20-Nov-18 19:11pm

"INNK-" is just a "decoration".

In your situation, the easiest thing to do is simply to put a "label" (containing "INNK-") on your form ... in front of the "id" field; e.g. INNK-[...].

Then you don't have to worry about "parsing"; just convert the "id" between int / string.

You wouldn't need to worry about "incrementing" either if you used an IDENTITY column (for the id).
 
Share this answer
 
Comments
Member 14061303 20-Nov-18 14:25pm    
Good suggestion. but i want to do it with code.
Based on your desire to have this text in your DB, I would recommend utilizing an integer based identity column as Gerry suggested (I'll call it InvoiceID),
and have a second column (I'll call this InvoiceNumberText).
Then, to have your code do this; create a Trigger such as this to populate it
SQL
CREATE TRIGGER CalculateInvoiceNumberText
AFTER INSERT ON Invoices
BEGIN
  UPDATE Invoices
  SET InvoiceNumberText = 'INNK-' + Cast(new.InvoiceID as Text)
  WHERE InvoiceID = new.InvoiceID
END;
 
Share this answer
 
Try this one.
'INNK-' will not be saved in database. Only integer value will be saved in database.
DECLARE @NewInvoiceNumber varchar(30);

SELECT @NewInvoiceNumber = RIGHT(1000 + InvoiceNumber + 1, 3)
    FROM
    (
    SELECT CONVERT(INT,InvoiceNumber) AS InvoiceNumber FROM tableName
    ) Temp

UPDATE tableName SET InvoiceNumber = @NewInvoiceNumber
SELECT Invoice = 'INNK-'+@NewInvoiceNumber
 
Share this answer
 

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