Contact Form

Name

Email *

Message *

Follow on LinkedIn
Image

20. Displaying Categories from Database into Combobox in Product Form



In the previous episode, we have created methods to SELECT, INSERT, UPDATE and DELETE products in database.
In this episode, we will display all the categories from database into the combo box in our product form. For this, we will create a method to select categories from database and use it to display categories on combo box in our product form load event.
Methods and Events used in this episodes are as follows
WATCH THE VIDEO FIRST

Method to Select all the Categories and Display in Combobox

We will be using the same select method we create in previous episode, which is as follows

        public DataTable Select()
        {
            //Creating Database Connection
            SqlConnection conn = new SqlConnection(myconnstrng);

            DataTable dt = new DataTable();

            try
            {
                //Wrting SQL Query to get all the data from DAtabase
                string sql = "SELECT * FROM tbl_categories";

                SqlCommand cmd = new SqlCommand(sql, conn);

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                //Open DAtabase Connection
                conn.Open();
                //Adding the value from adapter to Data TAble dt
                adapter.Fill(dt);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }

            return dt;
        }

Event to Display Categories in Combobox when the form is loaded

First, create a product form load event and then paste the following code inside the event

          //Creating DAta Table to hold the categories from Database
            DataTable categoriesDT = cdal.Select();
            //Specify DataSource for Category ComboBox
            cmbCategory.DataSource = categoriesDT;
            //Specify Display Member and Value Member for Combobox
            cmbCategory.DisplayMember = "title";
            cmbCategory.ValueMember = "title";


Thank you so much for reading and watching my tutorial. If you want to see more posts like this, then don't forget to SUBSCRIBE.

If you want to START from the beginning of this course, then CLICK HERE.

BILLING AND INVENTORY MANAGEMENT SYSTEM IN C#

Comments