The Below Code Snippet Is To Retrieve And Display The Data From Database To ComboBox Using C#.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//For Data Connection
using System.Data.OleDb;
//For Configuration Manager Class
using System.Configuration;
namespace DatabaseDataToComboBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FillCombo();
}
private void FillCombo()
{
OleDbConnection con = new OleDbConnection(strConn);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT SNo, PersonNames FROM SavedNames";
DataSet objDs = new DataSet();
OleDbDataAdapter dAdapter = new OleDbDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
cmbNames.ValueMember = "SNo";
cmbNames.DisplayMember = "PersonNames";
cmbNames.DataSource = objDs.Tables[0];
//To Suggest And Show List When Typing In ComboBox
cmbNames.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmbNames.AutoCompleteSource = AutoCompleteSource.ListItems;
}
}
}
In App.Config File Write The Below Code Snippet For Database Connection To The Database From The Windows Applcation:
The Screenshot Shows The Output Of The Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//For Data Connection
using System.Data.OleDb;
//For Configuration Manager Class
using System.Configuration;
namespace DatabaseDataToComboBox
{
public partial class Form1 : Form
{
// To read the connection string into your code, use the ConfigurationManager class.
string strConn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FillCombo();
}
private void FillCombo()
{
OleDbConnection con = new OleDbConnection(strConn);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT SNo, PersonNames FROM SavedNames";
DataSet objDs = new DataSet();
OleDbDataAdapter dAdapter = new OleDbDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
cmbNames.ValueMember = "SNo";
cmbNames.DisplayMember = "PersonNames";
cmbNames.DataSource = objDs.Tables[0];
//To Suggest And Show List When Typing In ComboBox
cmbNames.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmbNames.AutoCompleteSource = AutoCompleteSource.ListItems;
}
}
}
In App.Config File Write The Below Code Snippet For Database Connection To The Database From The Windows Applcation:
<configuration>
<configSections>
</configSections>
<connectionStrings>
<addname="connectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\NamesDB.accdb"/>
</connectionStrings>
</configuration>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.