1

I'm creating a reservation system and this is my forms, I'm having a hard time trying to send the quantity of the products because I'm using multiple textboxes unlike the reservation id I can easily get the value because I'm only using one textbox enter image description here

     SqlConnection con = new SqlConnection(@"Data Source = MAAAYYK; Initial Catalog = dbFoodReservation; Integrated Security = True");
            SqlCommand cm = new SqlCommand();
            con.Open();          
            cm = new SqlCommand("Insert into tbl_Reservation(ReservationID,ReceiptID,Price,Qty)Values(@ReservationID, @ReceiptID, @Price, @Qty)", con);
            cm.Parameters.AddWithValue("@ReservationID", txtBoxreserveID.Text);
            cm.Parameters.AddWithValue("@ReceiptID", txtboxReceiptID.Text);
            cm.Parameters.AddWithValue("@Price", txtboxTotal.Text);
            cm.Parameters.AddWithValue("@Qty", txtfried.Text + txtbbq.Text);
            cm.ExecuteNonQuery();
            con.Close();

            

the blank one is there I don't know what to put because as you can see I'm in my form there are many textbox for the quantity

Is there a way for me to get those values like if loop or something?

I changed my code and found a way to send the value of qty but when I enter 1 and 1 it shows 11 in the database instead of 2

Here is the database

enter image description here

1
  • Do not use string concatenation to create an SQL command. Use parameterized statements. bobby-tables.com Commented May 17, 2022 at 2:20

1 Answer 1

2

You must convert text to Integer for Prevent concatenate strings.

// clean code
var intTxtfried = Convert.ToInt32(txtfried.Text);
var intTxtbbq = Convert.ToInt32(txtbbq.Text);
var totalQty = intTxtfried + intTxtbbq;

// other code ...

cm.Parameters.AddWithValue("@Qty", totalQty);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.