Hi

I have previously populated a listbox / dropdown list with data from a stored procedure in ASP.NET C# and called the method in the PageLoad so that the list would be available once the page loaded up.

However, now I want to do the same thing except I want to do it for a Windows Application and not a website. So, how can I make the listbox automatically display the results of the sproc?

public void getCompanyName() //Method to return list of company names and add to listbox
        {
            //Clear the listbox
            listBox_CompanyName.Items.Clear();
            //Create SQL command, declare SPROC and connection to database
            myCommand = new SqlCommand("get_CompanyName", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            //Open database connection
            myConnection.Open();

            //Execute myCommand into the reader and close the connection
            myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            while (myReader.Read())
            {
                //Loop through the reader appending each element to the listbox
                listBox_CompanyName.Items.Add(myReader.GetString(1));
               
            }
            
            myReader.Close();
            myCommand.Dispose();
        }

Has anyone any ideas please? :)

Add the same code to the form_shown

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.