Home > Mobile >  got System.FormatException: 'Input string was not in a correct format.' on C#
got System.FormatException: 'Input string was not in a correct format.' on C#

Time:03-03

i got error after can insert for 2nd times

enter image description here

my code is

private void btnInsert_Click(object sender, EventArgs e) {

        if (txtJumlah.Text == "")
        {
            MessageBox.Show("Harap Mengisi Jumlah Item !");
        }
        else
        {
            int stok = Convert.ToInt32(txtStok.Text);
            int jumlah = Convert.ToInt32(txtJumlah.Text);
            if (stok < jumlah)
            {
                MessageBox.Show("Sisa Stok Tidak Mencukupi");
            }
            else
            {
                if(guna2DataGridView2.Rows.Count == 0)
                {
                    Insert();
                }
                else
                {
                    InsertAda();
                    KA();
                }
            }
        }
            
    }
    public void Insert()
    {
        int jumlahawal = Convert.ToInt32(txtJumlah.Text);
        int harga = Convert.ToInt32(txtHarga.Text);
        guna2DataGridView2.Rows.Add(txtKodeBarang.Text, txtNamaBarang.Text, txtHarga.Text, txtJumlah.Text, harga * jumlahawal);
        KA();

    }
    public void InsertAda()
    {
        
        for (int i = 0; i < guna2DataGridView2.Rows.Count; i  )
        {
            if (guna2DataGridView2[0, i].Value.ToString() == txtKodeBarang.Text)
            {
                int harga = Convert.ToInt32(txtHarga.Text);
                guna2DataGridView2.Rows[i].Selected = true;
                int jumlahi = Convert.ToInt32(guna2DataGridView2.Rows[i].Cells[3].Value);
                int jumlahs = Convert.ToInt32(txtJumlah.Text);
                int stok = Convert.ToInt32(txtStok.Text);
                if(jumlahi   jumlahs > stok)
                {
                    MessageBox.Show("Sisa Stok Tidak Mencukupi");
                }
                else
                {
                    guna2DataGridView2.Rows[i].Cells[3].Value = jumlahi   jumlahs;
                    int totali = Convert.ToInt32(guna2DataGridView2.Rows[i].Cells[3].Value);
                    guna2DataGridView2.Rows[i].Cells[4].Value = totali * harga;
                }
                    
            }
            else
                    
            {
                Insert();
            }
        }
    }

the first insert() code will success, when datagridview have 1 row it will call InsertAda() this code will check if datagridview has row same text with textbox it will update the row, but if doesnt has it will call Insert() so when i insert another row this error appear,

CodePudding user response:

txtJumlah.Text is not an number.

Please consider the following example:

Console.WriteLine(Convert.ToInt32("1"));
Console.WriteLine(Convert.ToInt32("One"));

The output shows the error you encountered:

1
Unhandled exception. System.FormatException: Input string was not in a correct format.
   at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
   at System.Convert.ToInt32(String value)
   at Program.<Main>$(String[] args) in C:\git\games\Program.cs:line 2

CodePudding user response:

[enter image description here][1]

this when i first insert data it works [1]: https://i.stack.imgur.com/YT2G8.png

but when i insert new data and not same at previous data, it will error

  •  Tags:  
  • c#
  • Related