Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table in my data base with 4 columns Specie | Price | Stock | Country. and two drop down list, the first is Country once the country is selected the second drop down Shows the Specie allocated to the country. This all works well.

Problem

I have select index change that shows 2 label's with Price and Specie selected. problem being is that the label is not showing the correct print out of the country selected, I know its the query but I have tried several variations and can not seem to get it to work correct, advise or help will be much appreciated.

Code with query below

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selection_price = DdPetPist.SelectedValue;
        string selection_stock = DdPetPist.SelectedValue;
        string petPrice = string.Empty;
        string available = string.Empty;

        MySqlCommand cd_price = new MySqlCommand(String.Format("SELECT Price FROM Animals WHERE Specie ='{1}'", ddlcountry.Text, selection_price), cs);
        MySqlCommand cd_available = new MySqlCommand(String.Format("SELECT Stock FROM Animals WHERE Specie ='{0}'", ddlcountry.Text, selection_stock), cs);

        cs.Open();
        petPrice = Convert.ToString(cd_price.ExecuteScalar());
        available = Convert.ToString(cd_available.ExecuteScalar());
        cs.Close();

        PetPrice.Text = String.Format("Minimum Donation For A {0}  Is £{1}.", selection_price, petPrice);
        Availble.Text = String.Format("{0}'s Avalible {1} In Your Country.", selection_stock, available);
    } 
Posted
Updated 25-Nov-13 11:36am
v2

A critically important addition to Solution 1:

The term "concatenation" immediately makes your approach suspicious. And yes, it is wrong. Actually, you don't use sting concatenation to form your query using some user-supplied data. And this is good, string.Format has little to do with concatenation (with is inefficient when repeated, because strings are totally immutable). But the other thing is totally bad: you should not creates queries from the user input at all. It opens wide doors to the well-known exploit called SQL injection. This is how it works:
http://xkcd.com/327[^] :-).

What to do instead? Well, you need to use parametrized statements: http://msdn.microsoft.com/en-us/library/ff648339.aspx[^].

Please see my past answers for further explanations:
EROR IN UPATE in com.ExecuteNonQuery();[^],
hi name is not displaying in name?[^].

—SA
 
Share this answer
 
v3
Your query does not make sense. The string.Format is not being used correctly. It is a zero index function. In your WHERE clause you are checking if "Specie" is equal to "selection_price". Your SELECT statements should read like this:

MySqlCommand cd_price = new MySqlCommand(String.Format("SELECT Price FROM Animals WHERE Specie ='{0}'", value for specie), cs);
     MySqlCommand cd_available = new MySqlCommand(String.Format("SELECT Stock FROM Animals WHERE Specie ='{0}'", value for specie), cs);


You will need a valid value to check the "Specie" against or change your query to actually check for price in the WHERE clause.

Of course, you should not be concatenation strings into an SQL statement. You should be using parameterized queries.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-Nov-13 18:34pm    
Well, this a good catch and a useful explanation. However, it only can mislead the user into doing more serious wrong thing, which makes the code totally vulnerable to SQL injections.
Such way of making queries should never be use. Please see Solution 2 where I explain it.
(I did not vote, by the way.)
—SA
Richard C Bishop 25-Nov-13 18:37pm    
Agreed, hence my last sentence.

I should really be putting that information first so the OP realizes that before looking at the code I suppose. I guess I was correcting the use of the string.Format instead of all possible issues.

Once again, thank you for clarification.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900