Home > database >  is there a way that i can send values to database while using multiple textboxes?
is there a way that i can send values to database while using multiple textboxes?

Time:05-17

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 im using multiple textboxes unlike the reservation id i can easily get the value because im 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 dont know what to put because as you can see im 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?

**PS edit: ichanged 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

CodePudding user response:

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

CodePudding user response:

correct me if im wrong, but what you want to do is, when user key in the receiptid and other details, u want to submit it to the database right?. You can try to use the below code and see if it works based on your requirement. First, connect to db, create a query and open connection. Then get the input of user from the text with the parameter name and then execute it.

SqlConnection con = new SqlConnection(@"Data Source = MAAAYYK; Initial Catalog = dbFoodReservation; Integrated Security = True");
      SqlCommand cmd = new SqlCommand("Insert into tbl_Reservation(ReservationID,ReceiptID,Price, Qty)Values(@ReservationID,@ReceiptID,@Price,@Qty)END",con);;
        con.Open();
    cmd.Parameters.AddWithValue("@ReservationID",object name);
    cmd.Parameters.AddWithValue("@ReceiptID",object name);
    cmd.Parameters.AddWithValue("@Price",object name);
    cmd.Parameters.AddWithValue("@Qty",object name);
        cmd.ExecuteNonQuery();
        con.Close();
  • Related