-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?

4
  • 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? Commented Oct 27, 2015 at 10:06
  • 2
    Also add validation to your forms so that users can't type arbitrary text in textboxes meant for numeric data Commented Oct 27, 2015 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 Commented Oct 27, 2015 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. Commented Oct 27, 2015 at 10:24

2 Answers 2

6

Use should use like this code for convert:

command.Parameters.Add("@Qty", SqlDbType.Int).Value = Convert.ToInt32(qtychoiceTxt.Text); 
Sign up to request clarification or add additional context in comments.

7 Comments

this could bereak if the value of any of the fields are not int. I suggest using TryParse instead.
@AmitKumarGhosh This is simple convert and not validation.for validation we should handle the value also we can use int.TryParse
@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
This question is about conversion and not about validation.For validation we have many solution.
@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
|
0

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

3 Comments

this could bereak if the value of any of the fields are not int. Use TryParse instead
Not really - at this point it's better to throw an exception than record a payment of 0 quantity or 0 amount. TryParse should be used to validate the input befor the stored procedure is even called
we were just talking about the conversion not about the full functionality

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.