-5

I'm trying to do an insertion to a Transaction table in SQL Server from a certain textboxes and one label on C# Windows Form, with the following code:

AppProcess abc = new AppProcess();
SqlConnection sqlconn1 = abc.GetConn();

SqlCommand sqlinsert = new SqlCommand("insert into Payment values
(@TID,@EID,@CustID,@CFName,@CLName,@BID,@BName,@Price,@Qty,@TransDate)",
sqlconn1);

DataTable dtCustomer = new DataTable();
sqlconn1.Open();

sqlinsert.Parameters.Add(new SqlParameter("@TID", SqlDbType.VarChar, 10));
sqlinsert.Parameters.Add(new SqlParameter("@EID", SqlDbType.VarChar, 10));
sqlinsert.Parameters.Add(new SqlParameter("@CustID", SqlDbType.VarChar, 10));
sqlinsert.Parameters.Add(new SqlParameter("@CFName", SqlDbType.VarChar, 100));
sqlinsert.Parameters.Add(new SqlParameter("@CLName", SqlDbType.VarChar, 100));
sqlinsert.Parameters.Add(new SqlParameter("@BID", SqlDbType.VarChar, 10));
sqlinsert.Parameters.Add(new SqlParameter("@BName", SqlDbType.VarChar, 100));
sqlinsert.Parameters.Add(new SqlParameter("@Price", SqlDbType.Int));
sqlinsert.Parameters.Add(new SqlParameter("@Qty", SqlDbType.Int));
sqlinsert.Parameters.Add(new SqlParameter("@Transdate", SqlDbType.Date));

sqlinsert.Parameters["@TID"].Value = tidTxt.Text;
sqlinsert.Parameters["@EID"].Value = eidTxt.Text;
sqlinsert.Parameters["@CustID"].Value = cidTxt.Text;
sqlinsert.Parameters["@CFName"].Value = cfnameTxt.Text;
sqlinsert.Parameters["@CLName"].Value = clnameTxt.Text;
sqlinsert.Parameters["@BID"].Value = bidTxt.Text;
sqlinsert.Parameters["@BName"].Value = bnameTxt.Text;
sqlinsert.Parameters["@Price"].Value = label17.Text;
sqlinsert.Parameters["@Qty"].Value = qtychoiceTxt.Text;
sqlinsert.Parameters["@Transdate"].Value = TransDateTxt.Text;

sqlinsert.ExecuteNonQuery();

But when I run the app, it generates this error: click here to see what the error looks like

I'm confused which line triggers the error though. And how to convert the string to Int32?

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
zdrasvutye
  • 21
  • 2
  • 8
  • 1
    what do you get from `label17.Text` and `qtychoiceTxt.Text`? From the exception detail, it is obvious your passing in strings instead of ints causing a `FormatException`.please check if those field values can be casted to int? – Amit Kumar Ghosh Oct 27 '15 at 10:06
  • 2
    Also add validation to your forms so that users *can't* type arbitrary text in textboxes meant for numeric data – Panagiotis Kanavos Oct 27 '15 at 10:11
  • label17.Text is a total cost after the calculation of the quantity chosen on qtychoiceTxt.Text with the product price. I'm trying to save those values – zdrasvutye Oct 27 '15 at 10:11
  • 1
    Why are you using a `label` to show results? A label is there to label things. If you want to show the user a result, use a `TextBox` and set it to `ReadOnly`. – waka Oct 27 '15 at 10:24

2 Answers2

6

Use should use like this code for convert:

command.Parameters.Add("@Qty", SqlDbType.Int).Value = Convert.ToInt32(qtychoiceTxt.Text);
  • this could bereak if the value of any of the fields are not int. I suggest using `TryParse` instead. – Amit Kumar Ghosh Oct 27 '15 at 10:10
  • @AmitKumarGhosh This is simple convert and not validation.for validation we should handle the value also we can use int.TryParse –  Oct 27 '15 at 10:11
  • 1
    @AmitKumarGhosh breaking *is* appropriate here. Better to ask the user to fix the data than store 0 by default. An accidental "payment" of 0 is not a happy situation – Panagiotis Kanavos Oct 27 '15 at 10:12
  • So you think `Convert.ToInt32("12.00")` is fine here? – Amit Kumar Ghosh Oct 27 '15 at 10:12
  • 1
    This question is about conversion and not about validation.For validation we have many solution. –  Oct 27 '15 at 10:14
  • 2
    @AmitKumarGhosh what is fine, is validators on the form, validation on the data-binding code (all this is available OOTB), and finally, yes, throw an exception and warn the user if previous validations fail. If my company payed me 0 Euros by accident, I would *not* be happy at all – Panagiotis Kanavos Oct 27 '15 at 10:14
  • `Convert.ToInt32("sometext")` definitly is NOT fine. If we convert, we ALWAYS have to check if the conversion actually can be done or not. So either use `try catch` or (even better) `Int32.TryParse`. – waka Oct 27 '15 at 10:22
0

You have to Convert "label17.Text" and "qtychoiceTxt.Text" to integer by using Convert.ToInt32("string")

Varun Vasishtha
  • 461
  • 2
  • 9