System.FormatException: 'Input string was not in a correct format.' in C#

Godawelage Erangi Udeshika 1 Reputation point
2022-05-20T18:46:53.06+00:00

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace AyuBo_Pvt
{
public partial class Rent : Form
{
public Rent()
{
InitializeComponent();
}

    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-PJOEEF0\MSSQLSERVER01;Initial Catalog=Ayubo;Integrated Security=True");
    private void populate()
    {
        con.Open();
        string query = "Select * from Vehicle_details";
        SqlDataAdapter dp = new SqlDataAdapter(query, con);
        SqlCommandBuilder builder = new SqlCommandBuilder(dp);
        var ds = new DataSet();
        dp.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
        con.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        DateTime date1 = dateTimePicker1.Value.Date;
        DateTime date2 = dateTimePicker2.Value.Date;

        int dateiff = ((TimeSpan)(date2 - date1)).Days;
        textBox8.Text = dateiff.ToString();

        int months, weeks, days, remainder;
        int gap = Convert.ToInt32(textBox8.Text);
        months = gap / 30;
        remainder = gap % 30;
        weeks = remainder / 7;
        days = remainder % 7;
        textBox7.Text = days.ToString();
        textBox4.Text = weeks.ToString();
        textBox6.Text = months.ToString();
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {

    }

    private void label8_Click(object sender, EventArgs e)
    {

    }

    private void Rent_Load(object sender, EventArgs e)
    {
        populate();
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        int index = e.RowIndex;
        DataGridViewRow SelectedRow = dataGridView1.Rows[index];
        textBox3.Text = SelectedRow.Cells[0].Value.ToString();
        textBox12.Text = SelectedRow.Cells[1].Value.ToString();
        textBox9.Text = SelectedRow.Cells[2].Value.ToString();
        textBox11.Text = SelectedRow.Cells[3].Value.ToString();
        textBox2.Text = SelectedRow.Cells[4].Value.ToString();
        textBox1.Text = SelectedRow.Cells[5].Value.ToString();
        textBox10.Text = SelectedRow.Cells[6].Value.ToString();
        textBox5.Text = SelectedRow.Cells[7].Value.ToString();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        int day = Convert.ToInt32(textBox7.Text);
        int daily = Convert.ToInt32(textBox9.Text);

        int week = Convert.ToInt32(textBox4.Text);
        int weekly = Convert.ToInt32(textBox11.Text);

        int month = Convert.ToInt32(textBox6.Text);
        int monthly = Convert.ToInt32(textBox10.Text);

        int nbDays = Convert.ToInt32(textBox8.Text);
        int driverRate = Convert.ToInt32(textBox5.Text);

        label12.Text = Convert.ToString((day * daily) + (week * weekly) + (month * monthly) + (nbDays * driverRate));
        label13.Text = Convert.ToString((day * daily) + (week * weekly) + (month * monthly));

    }

    private void textBox13_TextChanged(object sender, EventArgs e)
    {

    }

    private void btnHome_Click(object sender, EventArgs e)
    {
        Home_Page hp = new Home_Page();
        hp.Show();
        this.Hide();
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace AyuBo_Pvt
{
public partial class Rent : Form
{
public Rent()
{
InitializeComponent();
}

    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-PJOEEF0\MSSQLSERVER01;Initial Catalog=Ayubo;Integrated Security=True");
    private void populate()
    {
        con.Open();
        string query = "Select * from Vehicle_details";
        SqlDataAdapter dp = new SqlDataAdapter(query, con);
        SqlCommandBuilder builder = new SqlCommandBuilder(dp);
        var ds = new DataSet();
        dp.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
        con.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        DateTime date1 = dateTimePicker1.Value.Date;
        DateTime date2 = dateTimePicker2.Value.Date;

        int dateiff = ((TimeSpan)(date2 - date1)).Days;
        textBox8.Text = dateiff.ToString();

        int months, weeks, days, remainder;
        int gap = Convert.ToInt32(textBox8.Text);
        months = gap / 30;
        remainder = gap % 30;
        weeks = remainder / 7;
        days = remainder % 7;
        textBox7.Text = days.ToString();
        textBox4.Text = weeks.ToString();
        textBox6.Text = months.ToString();
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {

    }

    private void label8_Click(object sender, EventArgs e)
    {

    }

    private void Rent_Load(object sender, EventArgs e)
    {
        populate();
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        int index = e.RowIndex;
        DataGridViewRow SelectedRow = dataGridView1.Rows[index];
        textBox3.Text = SelectedRow.Cells[0].Value.ToString();
        textBox12.Text = SelectedRow.Cells[1].Value.ToString();
        textBox9.Text = SelectedRow.Cells[2].Value.ToString();
        textBox11.Text = SelectedRow.Cells[3].Value.ToString();
        textBox2.Text = SelectedRow.Cells[4].Value.ToString();
        textBox1.Text = SelectedRow.Cells[5].Value.ToString();
        textBox10.Text = SelectedRow.Cells[6].Value.ToString();
        textBox5.Text = SelectedRow.Cells[7].Value.ToString();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        int day = Convert.ToInt32(textBox7.Text);
        int daily = Convert.ToInt32(textBox9.Text);

        int week = Convert.ToInt32(textBox4.Text);
        int weekly = Convert.ToInt32(textBox11.Text);

        int month = Convert.ToInt32(textBox6.Text);
        int monthly = Convert.ToInt32(textBox10.Text);

        int nbDays = Convert.ToInt32(textBox8.Text);
        int driverRate = Convert.ToInt32(textBox5.Text);

        label12.Text = Convert.ToString((day * daily) + (week * weekly) + (month * monthly) + (nbDays * driverRate));
        label13.Text = Convert.ToString((day * daily) + (week * weekly) + (month * monthly));

    }

    private void textBox13_TextChanged(object sender, EventArgs e)
    {

    }

    private void btnHome_Click(object sender, EventArgs e)
    {
        Home_Page hp = new Home_Page();
        hp.Show();
        this.Hide();
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,273 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,551 Reputation points
    2022-05-20T20:40:49.51+00:00

    Not knowing where the error happened here are some tips

    When dealing with a TextBox that should be an int do not allow invalid entries e.g. disallow non in values then use intTryParse to weed out empty string and decimal/double.

    private void DayTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46))
        {
            e.Handled = true;
            return;
        }
    
        if (e.KeyChar == 46 && (sender as TextBox).Text.IndexOf(e.KeyChar) != -1)
        {
            e.Handled = true;
        }
    }
    
    private void GetButton_Click(object sender, EventArgs e)
    {
    
        if (int.TryParse(DayTextBox.Text, out var day))
        {
            Debug.WriteLine(day.ToString());
        }
        else
        {
            Debug.WriteLine($"{nameof(DayTextBox)} is empty or Invalid");
        }
    }
    

    Next up, when you have set the DataSource for the DataGridView create a BindingSource, assign your DataTable to it's DataSource. The BindingSource will now be used as the DataSource for the DataGridView.

    To get the current row in the DataGridView use

    DataRow current = ((DataRowView)_bindingSource.Current).Row;
    

    Example to get field data

    var productId = current.Field<int>("ProductID");
    var productName = current.Field<string>("ProductName");
    

    Not sure if there is a current record use

    if (_bindingSource.Current is null)
    {
        return;
    }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 70,776 Reputation points
    2022-05-20T19:42:40.317+00:00

    any of the Convert.ToInt32() will fail if the string is blank or not a valid format

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.