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

        }

8 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. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

    ReplyDelete
  3. What is web and digital design?
    The aim of the Master’s course in net and Digital design is that the creation of various skilled profiles with in the web media design space that ar ready to set up, turn out and share through the online communities a replacement generation of content and services in an efficient manner. If you want courses oriented any details to contact us: Digital Marketing Training Institute

    ReplyDelete
  4. Awesome post, Great to Land here, Keep going, have a great Success!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    we are providing Elearning Company in Chennai since
    Digital Learning Solutions is a big boom to the Students and employee Engagement
    Mycoach Learning is the best certified Elearning Company, which offers
    Mycoach LMS

    ReplyDelete
  5. 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

Add your comments here

Back to Top