Home Application Develop How to Fill Combobox from Database C# Tutorial

How to Fill Combobox from Database C# Tutorial

24
0

Today, We Will Learn How to Fill Combobox from Database C#, I think, more than beginners facing a problem to fill the Combobox from Database. I have seen different videos, but they are made by simple usage of Combobox. So, I, want to share How to Fill Combobox from Database C#. Recently, I have shared How to Connect C# with Database, kindly watch first it, then you will easily understand.

Basically, Combobox is a collection, you can store multiple records in single Combobox.  You can store database values in Combobox to use different methods. There are many ways such as without Database, Use LINQ to SQL Connection to store Value in Combobox, Use String Connection to Store Value in Combobox and more.

how-to-fill-combobox-from-database-c

But, I will teach you to store the value in Combobox with usage string database connection. String connection is a very simple method to display database value in Combobox easily. And also you can LINQ to SQL Connection to display database values in Combobox.

How to Link Combobox with Database Values

I have, shared instructions how you do it with step by step. You can follow on my below instruction than you can easily link Combobox with Database Values. Then you will able to Fill Combobox with Database in C# as well.

  • Make Single Project and Choose Windows Form.
  • Drag and Drop Combobox and Use Event Selected Indexed Change.
  • Import SQL Server Library (using System.Data.SqlClient).
  • Use Below Codes.

namespace ComboBox
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection(@”Data Source=ASIF\SQLEXPRESS;Initial Catalog=Training; Integrated Security=True”);
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
try
{
conn.Open();
SqlCommand sc = new SqlCommand(“select (Name) from Employees”, conn);
SqlDataReader Reader;
Reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add(“Name”, typeof(string));
dt.Load(Reader);
comboBox1.ValueMember = “Name”;
comboBox1.DataSource = dt;
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string check = comboBox1.SelectedValue.ToString();
}
}
}

Then, your Database Values Display in Combobox as well. That’s simple procedure to display Database Value in Combobox. Now, If you have any problem to understand it, kindly watch my video tutorial, I have made the video tutorial on this topic. I hope video tutorial is helpful and beneficial for everyone.

How to Fill Combobox from Database C# Tutorial

Thanks for Watch Complete video, after watch this video tutorial, I hope you have understood as well, and you will learn it something new. Kindly, share my content on social networks, and subscribe my website social networks pages like Facebook, Google+, Twitter, YouTube and Email for post updates.

If you have any question / suggestion about this topic, comment or email me after received your email or comment, I will reply with your answer maximum 24 hours. Thanks Again for a visit, Read my Complete Article and Watch my Videos. Keep touch my website, I will share very soon application like  Android Application very soon.

Subscribe My YouTube Channel For Get Updates 🙂