Saturday, 29 March 2014
How to Connectivity with Ms-Access 2007 with C# .Net
23:21
1 comment
Connectivity
with Ms-Access 2007 with C# .Net
Note:
In this topic we will discuss how to apply insert
delete update search command in C# .Net with Microsoft Access 2007.
Step
1:
First we will go All Program -> Microsoft Office-> Microsoft Office Access 2007 after this we follow this step which is
given below down.....
Step
2:
after this we create table which name Student that contains four field SrNo->Number,
Name->Text,
Dob->DateTime,Course->Text this
arrow is indicating the datatype
of each Colum of table.....
Step-3: after
this we take a windows Application and follow this step……..
Code for Form1.cs
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;
//using this
namespace....................
using System.Data.OleDb;
using System.Text.RegularExpressions;
namespace ConnectivitywithAccess
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection con;
private void Form1_Load(object sender, EventArgs
e)
{
//connection String................................
con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=D:\MyCollege.accdb;Persist Security Info=True;Jet OLEDB:Database Password=kush;");
DataGridBind();
}
// code for how to save data in
Microsoft Access 2007.............
private void
bntsave_Click(object sender, EventArgs e)
{
con.Open();
OleDbCommand cmd = new OleDbCommand("insert into Student values(" + Convert.ToInt32(txtsrno.Text) + ",'" + txtname.Text + "','" + Convert.ToDateTime(txtdob.Text)
+ "','" + txtcourse.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
DataGridBind();
MessageBox.Show("Data
Save Successfully");
TextClear();
}
//code for how to search data with
SrNo…..
private void
btnsearch_Click(object sender, EventArgs e)
{
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from Student where SrNo=" + Convert.ToInt32(txtsrno.Text) + "", con);
OleDbDataReader dr =
cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
txtname.Text
= dr["Name"].ToString();
txtdob.Text
= dr.GetDateTime(2).ToShortDateString();
txtcourse.Text
= dr[3].ToString();
}
else
{
MessageBox.Show("SrNo
=" + txtsrno.Text + " is not found");
}
con.Close();
}
//code for how to delete data with
SrNo…..
private void
btnDelete_Click(object sender, EventArgs e)
{
con.Open();
OleDbCommand cmd = new OleDbCommand("delete from Student where SrNo="+Convert.ToInt32(txtsrno.Text)+"", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("SrNo
=" + txtsrno.Text + " is deleted successfully");
}
//code for how bind dataGrid with Table ………….with Disconnected Mode
private void DataGridBind()
{
// con.Open();
OleDbCommand cmd = new OleDbCommand("select * from Student order by SrNo",
con);
//OleDbDataReader dr = cmd.ExecuteReader(); //with Connected Mode
//dr.Read();
//DataTable dt = new DataTable();
//dt.Load(dr);//
//dataGridView1.DataSource = dt;
//con.Close();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource
= ds.Tables[0];
}
//code for
how to clear all TextBox
private void
TextClear()
{
foreach(Control c in this.Controls)
{
if(c is TextBox)
//if (c.GetType() == typeof(TextBox))
{
c.Text
= "";
}
}
}
// code for
only insert numeric value in
TextBox .......................
private void
txtsrno_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsNumber(e.KeyChar)
|| e.KeyChar == 48 || e.KeyChar == 57)
{
e.Handled
= false;
}
else
{
e.Handled
= true;
}
}
}
}
//Result....
Saturday, 22 March 2014
How to binding DataGrid in WPF with xml, Sql table and Class Property
22:12
No comments
How
to binding DataGrid in WPF with xml, Sql table and Class Property
Note: In this topic we will discuss about how to
bind DataGrid in WPF.
First Method…………………………….
1. First we bind this dataGrid with Class and Property
then we take a separate class in our application which name is Student.cs
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
namespace BindDataGridinWPF
{
class Student
{
public int RollNo { get; set; }
public string Name { get; set; }
public string Gender{ get; set; }
public string Course { get; set;}
}
}
After we
create MainWindow.xaml……………………………
Code with MainWindow.xaml
………………………………
<Window x:Class="BindDataGridinWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Background="Azure">
<Grid>
<DataGrid x:Name="studentdatagrid" HorizontalAlignment="Left" Margin="87,78,0,0" VerticalAlignment="Top" Height="111" Width="275" Background="Beige"/>
</Grid>
</Window>
Code with MainWindow.xaml.cs
……………………………
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
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.Navigation;
using
System.Windows.Shapes;
namespace BindDataGridinWPF
{
/// <summary>
/// Interaction logic
for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded +=
MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
studentdatagrid.ItemsSource =
new List<Student>
{
new Student{RollNo=1,Name="Kush
Tiwari",Gender="Male",Course="Mca"},
new Student{RollNo=2,Name="Somesh
Katiyar",Gender="Male",Course="B.Tech"},
new Student{RollNo=3,Name="Udayan
Maiti",Gender="Male",Course="Mca"},
new Student{RollNo=4,Name="Ravinder
Goel",Gender="Male",Course="M.Tech"}
};
}
}
}
Output:
Second Method…………………………….
Second method we bind this DataGrid with using XMLDataProvider to load and bind xml file to WPF
Data Grid. XmlDataProvider exposes Source and XPath properties. Source property
can be used to provide xml file name while XPath property used to specify the
element name to generate collection.
First step we take Folder
which name is Data and add Student.xml as shown in below picture
Code for Student.xml………………..
<?xml version="1.0" encoding="utf-8" ?>
<Students>
<Student RollNo="1" Name="Somesh Katiyar" Course="B.Tech" City="Kanpur" MobileNo="9911992345" />
<Student RollNo="2" Name="Manoj Yadav" Course="M.A." City="Ballia" MobileNo="3411992345" />
<Student RollNo="3" Name="Kush Tiwari" Course="Mca" City="Varanasi" MobileNo="7711992345" />
<Student RollNo="4" Name="Udayan Maiti" Course="M.Tech" City="Kolkatta" MobileNo="9900992345" />
<Student RollNo="5" Name="Vinay Singh" Course="B.A" City="Noida" MobileNo="9911789234" />
</Students>
Code with Window1.xaml ………………………………
<Window x:Class="BindDataGridinWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="400">
<Window.Resources>
<XmlDataProvider Source="Data\Student.xml" XPath="Students" x:Key="stu"
/>
</Window.Resources>
<Grid Width="325" Height="150" Background="Azure" Margin="11,10,10,10">
<DataGrid Name="StudentGrid" AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource stu},XPath=*}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=@RollNo}" Header="RollNo"
/>
<DataGridTextColumn Binding="{Binding XPath=@Name}" Header="Name"
/>
<DataGridTextColumn Binding="{Binding XPath=@Course}" Header="Course"
/>
<DataGridTextColumn Binding="{Binding XPath=@City}" Header="City"
/>
<DataGridTextColumn Binding="{Binding XPath=@MobileNo}" Header="Mobile
No" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Result:
Thrid Method…………………………….
Third method we bind this DataGrid with using Ado.Net concept to load and bind table records to WPF Data
Grid…………………………..
-- Sql Query.........................
create database WpfConcept
use WpfConcept
create table Student(RollNo int primary key,Name varchar(50),Gender varchar(50),Course varchar(50))
Code with Window2.xaml ………………………………
<Window x:Class="BindDataGridinWPF.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Bind DataGrid
with Sql Table"
Height="300" Width="300">
<Grid>
<DataGrid x:Name="datagrid1" Width="235" Height="150" Background="Salmon"
/>
</Grid>
</Window>
Code with Window2.xaml.cs
………………………………
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
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 this namespace
using System.Data;
using
System.Data.SqlClient;
namespace BindDataGridinWPF
{
/// <summary>
/// Interaction logic
for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
this.Loaded +=
Window2_Loaded;
}
void Window2_Loaded(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection("Data
Source=KUSH-PC;Initial Catalog=WpfConcept;User ID=sa;Password=tiwari");
SqlCommand cmd = new SqlCommand("Select *
from Student",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Student");
//datagrid.ItemsSource =
ds.Tables["MyDetails"].DefaultView;
datagrid1.ItemsSource = new DataView(ds.Tables["Student"]);
}
}
}
Result:
Subscribe to:
Posts (Atom)




.jpg)














