user19131976
user19131976

Reputation:

Is there a way that I can send values to database while using multiple textboxes?

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

Upvotes: 1

Views: 63

Answers (1)

thisisnabi
thisisnabi

Reputation: 1200

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);

Upvotes: 2

Related Questions