ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Populate ComboBox from Business class using C# in Windows Application<p>Hi&nbsp;PSowmiya,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>I have made use of the following table Customers with the schema as follows.</p> <p><img src="" alt="" width="384" height="137" /></p> <p>I have already inserted few records in the table.</p> <p><img src="" alt="" width="340" height="156" /></p> <p>You can download the database table SQL by clicking the download link below.</p> <p><a href="">Download SQL file</a></p> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p>Static Class to return items for populating ComboBox.</p> <pre class="brush: csharp">using System.Collections.Generic; using System.Data.SqlClient; public static class RetrieveModule { public static List&lt;string&gt; retrieveCountries() { List&lt;string&gt; countries = new List&lt;string&gt;(); SqlConnection conn = new SqlConnection(@&#34;Data Source=.;Initial Catalog=Test;Uid=sa;pwd=pass@123;&#34;); string Sql = &#34;SELECT DISTINCT Country FROM Customers;&#34;; SqlCommand cmd = new SqlCommand(Sql, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { countries.Add(dr[&#34;Country&#34;].ToString()); } conn.Close(); return countries; } }</pre> <p>Accessing the static class in form</p> <pre class="brush: csharp">public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { List&lt;string&gt; countries = RetrieveModule.retrieveCountries(); foreach (string country in countries) { cbCountries.Items.Add(country); } } private void Submit(object sender, EventArgs e) { string message = &#34;Country: &#34; + cbCountries.Text; MessageBox.Show(message); } }</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="" alt="" width="149" height="125" /></span></strong></p>, 30 Oct 2018 07:55:42 GMT