Beep
Beep

Reputation: 2823

Switch statement C# using data from MYSQL databse

OK, so I have a column in MYSQL data base, the column called specie in there I have dog,cat,and rabbit. I then have a drop down list that is populated with the data from specie. I need a switch statement that will display a Label for each pet selected. e.g. if user selects rabbit, the rabbit cost £00.00

here is my code for the drop down list

 public partial class _Default : Page
{

    DataSet ds = new DataSet();
    MySqlConnection cs = new MySqlConnection(@"SERVER= 000.000.00.000;username=*****;password=*****; Initial Catalog = mydb");
    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();
    }

Upvotes: 0

Views: 131

Answers (1)

Binita Mehta
Binita Mehta

Reputation: 339

I am assuming you have the cost of each animal stored in the database as well. If so, here is how I would do this:

  1. Populate the dropdown list like you currently do.
  2. In .NET you can specify AutoPostBack="true" and onSelectedIndexChanged="someMethod". The former will post back to the server on dropdown change and the latter will call "someMethod".
  3. This "someMethod" can then look up the cost in the database for the species selected, and populate the label.

Makes sense?

Upvotes: 1

Related Questions