Connecting DropDownList to mysql in asp.net using c#

I created aboutcodes data base having reportcard table as show below



If I want to make subject column to load in DropDownList than use below function and call subjectname() Function in Page_Load Function

Here DropDownList1 is Id of DropDownList

public void subjectname()
        {
            string MyConString = "SERVER=localhost;" +
                       "DATABASE=aboutcodes;" +
                       "UID=root;" +
                       "PASSWORD=;";

            /* speed is database name, root is mysql username, test is mysql password */

            using (MySqlConnection connection = new MySqlConnection(MyConString))
            {
                MySqlCommand command = connection.CreateCommand();
                try
                {
                    // open connection
                    connection.Open();
                    // reportcard is mysql table name

                    string cmdText = "SELECT * FROM  `reportcard` ";

                    MySqlCommand cmd = new MySqlCommand(cmdText, connection);

                    MySqlDataReader ddlValues;
                    ddlValues = cmd.ExecuteReader();

                    //subject is column name

                    DropDownList1.DataSource = ddlValues;
                    DropDownList1.DataValueField = "subject";
                    DropDownList1.DataTextField = "subject";
                    DropDownList1.DataBind();
                    //close connection
                    connection.Close();
                }
                catch
                {

                    Label1.Text = "Error In Connection";


                }
            }//end of use of use

        }

13 Comments

  1. Nice article. I feel that u can do it a touch better as this is connected architecture.
    import a dll file called microsoft.applicationblocks.data
    and instead of string cmdText = "SELECT * FROM `reportcard` MySqlCommand cmd = new MySqlCommand(cmdText, connection);
    MySqlDataReader ddlValues;
    ddlValues = cmd.ExecuteReader();
    u can use
    dataset ds=sqlhelper.executedataset(connectionname,"Query");

    ReplyDelete
  2. I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating us.
    Python Training in Chennai
    Python Course in Chennai
    Advanced Java Training in Chennai
    Hadoop Course in Chennai
    Big Data Analytics Courses in Chennai
    Big Data Course in Chennai
    Selenium Training in Chennai
    Python Training in Tambaram


    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. FOXZ88.NET online casino website Global standard 2020-2021
    คาสิโนออนไลน์


    Betting online gambling reminiscent of UFASCR.COM Baccarat.
    ufabet


    UFABET football betting website, the big brother of all UEFA networks, UFADNA, with an update The first modern system in 2021
    ufa


    Web football i99PRO online lottery casino apply today for free 5000 bonus
    เว็บบอล


    Kardinal Stick Siam - Relx a great promotion. Express delivery in 3 hours.
    relx

    ReplyDelete
  5. a fast Online football betting Online casinos Baccarat online In an easy fashion, i99PRO 24-hour service can enable you to pleasantly bet on boxing. And in addition boasts a total service Open just one site, it could do all that. As the center wants actually There's also a Call Center staff members that are manufactured 24 hours 1 day plus the site of ours is a wide choice of expert services including internet football betting. Boxing betting on the internet and casinos that can be starting to be famous in this dimensions. And still popular as among the top. Our i99PRO site in addition has garnered the confidence and total satisfaction of many gambling enthusiasts. And in addition offers a wide choice of football, lottery, basketball, boxing, internet casinos And there are a lot more additional sorts of sports awaiting one to come in.ilver fox
    บาคาร่า
    สล็อต
    ufa
    แทงบอล


    ReplyDelete

Add your comments here

Back to Top