Beep
Beep

Reputation: 2823

Insert two values into one column including a multiplication C#

Hi all I am Having some problem with a program would appreciate some help.

  1. Firstly I am trying to take Donation_euro.Text and times it by 0.83 to get the conversion rate, I think I have done this correct but it dose not seem to be working. (reason for the conversion is that I can have only one currency in my database).

  2. Secondly I am trying to insert donation_total and donation.Text into the same column, this will give my user the choice of paying in Euro's or Pound's. but it seems I can not do this the way I have tried, is there a way to fix this to get it to work?

                Double donation_euro = Convert.ToDouble(Donation_euro.Text);
                Double convertion_rate = 0.83;
                Double donation_total = donation_euro * convertion_rate;
    
                da.InsertCommand = new MySqlCommand("INSERT INTO Customer(Donation,donation_total)VALUES (@Donation)", cs);
                {
    
                    da.InsertCommand.Parameters.AddWithValue("@Donation", donation.Text + donation_total);
                } 
    

note: Donation_euro.Text and donation.Text are both text boxes and I have my table column set to double

Upvotes: 1

Views: 1100

Answers (2)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56717

You're trying to fill to fields using the INSERT, but you're only providing one value in the VALUES section of the statement. This gives an error in the call, as it is not valid SQL.

Change it as follows:

da.InsertCommand = new MySqlCommand("INSERT INTO Customer(Donation,donation_total) VALUES (@Donation, @Total)", cs);
{
    da.InsertCommand.Parameters.AddWithValue("@Donation", donation.Text);
    da.InsertCommand.Parameters.AddwithValue("@Total", donation_total);
} 

Oh: And +1 for using parameterized queries!

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172538

Try this:

da.InsertCommand.Parameters.AddWithValue("@Donation", donation_total);

instead of:

da.InsertCommand.Parameters.AddWithValue("@Donation", donation.Text + donation_total);

Also your insert statement expects the second parameter.(Hope you are providing that in the actual code)

da.InsertCommand = new MySqlCommand("INSERT INTO Customer(Donation,donation_total)VALUES (@Donation)", cs);

Upvotes: 1

Related Questions