'Input string was not in a correct format.'

  • Thread starter Thread starter IulianG06
  • Start date Start date
I

IulianG06

Guest
I tried to make a code to insert all my data from a grid into a table. In the grid I display what I need, it's not the problem, or it does not give an error

Displays this error:


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

My table:

CREATE TABLE [dbo].[concediati] ( [Id] INT IDENTITY (1, 1) NOT NULL, [nume] VARCHAR (50) NULL, [prenume] VARCHAR (50) NULL, [idclient] INT NULL, [idrent] INT NULL, [idcar] INT NULL, PRIMARY KEY CLUSTERED ([Id] ASC));



string StrQuery;
try
{
using (SqlConnection conn = new SqlConnection(stringcon))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
conn.Open();
for (int i = 1; i < bunifuCustomDataGrid2.Rows.Count; i++)
{


StrQuery = @"INSERT INTO concediati(nume,prenume,idcar,idrent,idclient) VALUES (@name,@lastname,@car,@rent,@client)";
comm.Parameters.AddWithValue("@name", Convert.ToString(bunifuCustomDataGrid2.Rows.Cells["firstname"].ToString()));
comm.Parameters.AddWithValue("@lastname", Convert.ToString(bunifuCustomDataGrid2.Rows.Cells["lastname"].ToString()));
comm.Parameters.AddWithValue("@car", Convert.ToInt32(bunifuCustomDataGrid2.Rows.Cells["CARS"].ToString()));
comm.Parameters.AddWithValue("@rent", Convert.ToInt32(bunifuCustomDataGrid2.Rows.Cells["RENT"].ToString()));
comm.Parameters.AddWithValue("@client", Convert.ToInt32(bunifuCustomDataGrid2.Rows.Cells["CLIENT"].ToString()));



comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
}
}

Continue reading...
 
Back
Top