Stack Exchange
what's wrong with my C# codes in INSERT command with Parameter?
my friends , what's wrong with these codes that it appear "Input string was not in a correct format." Error ?????????
it appears in lines witch start with *
who can help me?
 try
        {
            using (SqlConnection con = new SqlConnection(connect))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO fact_info(fact_id, druge,price,amount,total,selldate) VALUES (@C1, @C2, @C3, @C4, @C5 ,@C6)", con))
                {
                    cmd.Parameters.Add(new SqlParameter("@C1", SqlDbType.Int ));
                    cmd.Parameters.Add(new SqlParameter("@C2", SqlDbType.NVarChar));
                    cmd.Parameters.Add(new SqlParameter("@C3", SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@C4", SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@C5", SqlDbType.Int));
                    cmd.Parameters.Add(new SqlParameter("@C6", SqlDbType.NVarChar ));
                    con.Open();
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        if (!row.IsNewRow)
                        {
                            *****cmd.Parameters["@C1"].Value =Convert .ToInt32 (  label3.Text.Trim());
                            cmd.Parameters["@C2"].Value = row.Cells[1].Value.ToString ();*****
                            cmd.Parameters["@C3"].Value =Convert .ToInt32 ( row.Cells[2].Value.ToString ());
                            cmd.Parameters["@C4"].Value =Convert .ToInt32 ( row.Cells[3].Value.ToString ());
                            cmd.Parameters["@C5"].Value =Convert .ToInt32 ( row.Cells[4].Value.ToString ());
                            cmd.Parameters["@C6"].Value = label5.Text.Trim();
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
            }
        }
        finally
        {
            MessageBox.Show("done! ");
        }
    }


your label3.Text is not an integer representation. It can't be parsed into an integer.

When you've sorted that passing dates as strings is not a brill idea either. SellDate is a date/datetime isn't it?

Also, why are you using a loop?  There's only one query being executed, so it's only ever going to INSERT the last row in the table.

Label3.text has my FactID and I wanna convert it to Int

in My SQL table , i designed the Selldate as in NVarchar

Well seeing as it's a date. Undesign as NVarchar, and design it as a date. That's just a good way of crashing when someone puts 30th February in there. Or they have their system configured to american and then in your european system it says 2/28/2013 is not a date.

Which line throws the error?  If I were to guess, it's likely that the error is happening here:
Convert.ToInt32(label3.Text.Trim())

What is the runtime value of label3.Text?  If it's not a numeric (integer, specifically) value, then Convert.ToInt32 is going to throw an exception because it can't be converted to an int.  You might want to add some run-time checking on the value:
var factID = 0;
if (!int.TryParse(label3.Text, out factID))
{
    // The label doesn't have an integer.
    // Display an error to the user, perhaps?
    // Either way, don't continue because there will be an error.
}

If the label has an integer, factID will be that integer.  If it doesn't, you can handle the error condition here.

yes, that's it ! my table field type is "Int" and i converted the amount of Label3.text into Int32 . isn't true???

@Elena what is the value of Label3.Text? if the value is not a number (let's say it says "Foo" instead of something like "12" then what number would it convert "Foo" in to?)

@Elena: It's true that you'd need to use an integer value to insert into an integer column in the database.  The problem isn't with the database, it's with the value.  If Convert.ToInt32() is throwing the error then clearly label3.Text isn't an integer value.

The value in label3.Text is not a valid integer and so Convert.ToInt32 can't parse it into an int thus throws the error instead. Either handle the exception in a Try...Catch block or use Int32.TryParse instead.
