WPF Bind ComboBox using MS SQL database

WPF ComboBox SelectionChanged 001

WPF Bind ComboBox using SQL database. WPF Fill Combo Box using MS SQL Database.

WPF Bind ComboBox using SQL database 01

Code :

String connectionString = "Data Source=192.168.0.192;Initial Catalog=ParallelCodes;User ID=sa;Password=789";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("select * from Producttbl", con);
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cbbDays.ItemsSource = dt.DefaultView;
cbbDays.DisplayMemberPath = "ProName";
cbbDays.SelectedValuePath = "Id";
cmd.Dispose();
con.Close();

comboxBoxName.ItemsSource = DataTable.DefaultView will assign a DataSource to the ComboBox using DataTable.

comboxBoxName.DisplayMemberPath = "StringNameColumnName" will assign Display value to our WPF ComboxBox control.

comboxBox.SelectedValuePath = "StringNameColumnName" will assign Value member to our WPF ComboxBox control.

Code Implementation :

In your WPF Project, create a new WPF Form with below design code :

Window1.xaml:

<Window x:Class="WPFParallelCodes.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowState="Maximized">
<StackPanel Orientation="Vertical">

<TextBlock Margin="5" FontSize="20" HorizontalAlignment="Left">Select any day : </TextBlock>
<ComboBox Name="cbbDays" FontSize="20" Margin="5" MaxWidth="250" Width="250" HorizontalAlignment="Left"/>
<Button Margin="5" Name="btnSubmit" Content="Submit" FontSize="20" MaxWidth="100" HorizontalAlignment="Left" Padding="2" Click="btnSubmit_Click_1"/>
<TextBlock Margin="5" FontSize="20" Foreground="BlueViolet" FontWeight="ExtraBlack" Name="txtBSelectedDay">Hi</TextBlock>
</StackPanel>
</Window>

And edit its code file as below :

Window1.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace WPFParallelCodes
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
FillComboBox();
}

public void FillComboBox()
{
try
{
String connectionString = "Data Source=192.168.0.192;Initial Catalog=ParallelCodes;User ID=sa;Password=789";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("select * from Producttbl", con);
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cbbDays.ItemsSource = dt.DefaultView;
cbbDays.DisplayMemberPath = "ProName";
cbbDays.SelectedValuePath = "Id";
cmd.Dispose();
con.Close();
}
catch (Exception ex)
{
}
}

private void btnSubmit_Click_1(object sender, RoutedEventArgs e)
{
txtBSelectedDay.Text = "You Selected : " + cbbDays.Text.ToString() + " and its Id : " + cbbDays.SelectedValue.ToString();
}
}
}

SQL Database Script :

create database ParallelCodes
use ParallelCodes
create table Usertbl
(
Id int identity (1,1) not null,
UserId nvarchar(50) not null,
Password nvarchar(50) not null
)
insert into Usertbl (UserId,Password) values ('hitesh1120', '789')
create table Producttbl (
Id int identity (1,1) not null,
ProName nvarchar(50) not null,
ProDesc nvarchar(50) not null,
OnDate nvarchar(50) not null
)
truncate table Producttbl
insert into Producttbl (ProName,ProDesc,OnDate) values ('Samsung J7', 'Gamers targeted mobile from Samsung', '2015-10-17 19:56:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Samsung J5', 'Gamers targeted mobile from Samsung', '2015-10-17 19:56:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Xiaomi Redmi 2', 'Best Entry level smartphone from Xiaomi', '2015-10-17 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Apple iPhone 6s', 'Flagship device from APPLE', '2015-10-17 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Apple iPhone 6s Plus', 'Flagship device from APPLE', '2015-10-17 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Xiaomi Redmi Note 2', 'Best Entry level smartphone from Xiaomi', '2015-10-17 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Lenovo A6000', 'Best Entry level smartphone from Lenovo', '2015-10-17 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Motorola MOTO X Pure Edition', 'Best Android device right now!!', '2015-10-17 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Motorola MOTO E 2gen', 'Budget device from Motorola', '2015-10-17 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Motorola MOTO E 2gen 4G', 'Budget device from Motorola', '2015-10-17 19:59:00.000')
select * from Producttbl

Now run your application to get below result :

WPF Bind ComboBox using SQL database 01

See Also :

1.WPF DataGrid from SQL Database. WPF DataGrid Bind from Database.

2.WPF Textbox with Rounded Corners and great Style.

3.WPF Textbox Focus Background Change.

 


1 thought on “WPF Bind ComboBox using MS SQL database”

  1. Hi!
    Thank.
    Can you help me.
    txtBSelectedDay.Text I want show value at column “Id” and value a new column when submit button. how can i do.
    (in your port only show value ID colum)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.