.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> WPF >> Post New QuestionBookmark and Share Subscribe to Forum

database connection in WPF

Posted By: siri.k     Posted Date: April 18, 2011    Points:5   Category :WPF
hai can any tell me how to connect database in wpf

suppose i have an employee table and i want to get information from empl table to wpf application how can i write code to this plz ..........

Responses
Author: Mahesh             
Posted Date: April 18, 2011     Points: 20   

Hi,
Suppose "window1.xaml" code is as below


<Window x:Class="WindowsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowsApplication1" Height="277" Width="356">
<ListBox Width="200" Margin="10" ItemsSource="{Binding Path=emp}" Name="lstEmployee">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=empId}" />
<TextBlock Text="{Binding Path=empName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>

and Code behind file is as:

"window1.xaml.cs":

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

namespace WindowsApplication1

{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

public partial class Window1 : System.Windows.Window
{
public OleDbConnection oleCon;
public OleDbCommand oleComd;
string strSql = "SELECT * FROM emp";

public Window1()
{
InitializeComponent();
BindData();
}

public void viewButton_Click(object sender, RoutedEventArgs args)
{
//string strVal = peopleListBox.SelectedValue.ToString();
//MessageBox.Show(strVal);
}

public void BindData()
{
oleCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDb.mdb");
oleComd = new OleDbCommand(strSql,oleCon);
DataSet dtst = new DataSet();
OleDbDataAdapter adpt = new OleDbDataAdapter();
try
{
oleCon.Open();
adpt.SelectCommand = oleComd;
adpt.Fill(dtst,"emp");
lstEmployee.DataContext = dtst;
}

catch(Exception ex)
{
//lblMessage.Content = ex.Message;
}

finally
{
oleCon.Close();
}
}
}
}


Thanks
Mahesh

Author: siri.k             
Posted Date: April 18, 2011     Points: 20   

thanku mahesh for ur reply here u mention listbox hmm i had taken combobox but coding is same right .

Author: siri.k             
Posted Date: April 18, 2011     Points: 20   

thanku mahesh for ur reply here u mention listbox hmm i had taken combobox but coding is same right .

Author: Mahesh             
Posted Date: April 18, 2011     Points: 20   

u r most welcome....



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend