Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to build a program that if user selects something from dropdown then a label will show depending on what is selected.

EG:

selects Dog from dropdown the label will show "Dog is £20"

selects Cat from dropdown the label will show "Cat is £15"

I am not doing it correct and have been at it for hours any help would be much appreciated.

**Code so far**


C#
DataSet ds = new DataSet();
           MySqlConnection cs = new MySqlConnection(@"SERVER= 000.000.00.000;username=myusername;password=******; Initial Catalog = mydatabase");
           MySqlDataAdapter da = new MySqlDataAdapter();

           protected void Page_Load(object sender, EventArgs e)
           {
               MySqlCommand cd = new MySqlCommand("SELECT * FROM pets", cs);
               cs.Open();
               MySqlDataReader ddl = cd.ExecuteReader();
               DdPetPist.DataSource = ddl;
               DdPetPist.DataValueField = "Specie";
               DdPetPist.DataTextField = "Specie";
               DdPetPist.DataBind();
               cs.Close();
               cs.Dispose();
           }


**Select Index Change**

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MySqlCommand cd = new MySqlCommand("SELECT CAST(Specie AS varchar) + '#' + CAST(Specie_Price AS varchar) AS Specie FROM pets", cs);
            cs.Open();
            PetPrice.Text = Convert.ToString(cd.ExecuteReader());
            cs.Close();
        } 


Please any help will be grate I have been stuck on this problem for hours and would like to move on to my next task at hand.
Many Thanks.
Posted
Comments
BillWoodruff 21-Nov-13 19:54pm    
I don't see how you expect to get any result when the user selects an item in the DropDown: you're not doing anything in the SelectedIndexChanged EventHandler code to use the current selection in the DropDown.

Am I missing something ?

1 solution

Your problem is on these two lines:

C#
MySqlCommand cd = new MySqlCommand("SELECT CAST(Specie AS varchar) + '#' + CAST(Specie_Price AS varchar) AS Specie FROM pets", cs);

//And

PetPrice.Text = Convert.ToString(cd.ExecuteReader());


The first one will return all your records cast as the string concatenation, I suspect you only want one, so you need to have a WHERE clause in the SQL statement (WHERE Specie = 'Whatever').

The second part, ExecuteReader does just that, it creates an IDataReader that can page through the results of your query. When you cast it to a string, you just get back the data type as a string, what you want is:

C#
PetPrice.Text = Convert.ToString(cd.ExecuteScalar());


Which returns the first column of the first result in your query.
 
Share this answer
 

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