Tuesday, 21 August 2012

How to insert data into Ms Access 2007 using WPF || Inserting and Binding of data to GridView from Ms Access in WPF

Xamal.Cs Code :


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
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 SampleWpfApp
{
    public partial class MainWindow : Window
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\WPFDatabase.accdb");
        public MainWindow()
        {
            InitializeComponent();
            Bind();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            con.Open();
            OleDbCommand cmd = new OleDbCommand("BankTableInsert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@BankName", cmbBankName.SelectedIndex);
            cmd.Parameters.AddWithValue("@AccountNo", txtAcNo.Text);
            if (rbtnSavingsAc.IsChecked.Value)
            {
                cmd.Parameters.AddWithValue("@AccountType", rbtnSavingsAc.Content);
            }
            else if (rbtnCurrentAc.IsChecked.Value)
            {
                cmd.Parameters.AddWithValue("@AccountType", rbtnCurrentAc.Content);
            }
            else if (rbtnCreditCard.IsChecked.Value)
            {
                cmd.Parameters.AddWithValue("@AccountType", rbtnCreditCard.Content);
            }
            else
            {
                cmd.Parameters.AddWithValue("@AccountType", rbtnOther.Content);
            }


            cmd.Parameters.AddWithValue("@Balance", txtBalance.Text);
            cmd.Parameters.AddWithValue("@Date", System.DateTime.Now.ToShortDateString());

           int result = cmd.ExecuteNonQuery();
           con.Close();
            if (result > 0)
            {
                MessageBox.Show("Record inserted successfully");
            }
            else
            {
                MessageBox.Show("Error occured, please try again", "Alert");
            }
            Bind();
        }

        private void Bind()
        {

            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter("select * from BankTable”, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            BanksetupGrid.ItemsSource = dt.DefaultView;
            con.Close();   
        }  
    }
}


OutPut :

How to create a sample WPF Sample Application using C#.Net.

XAML Code :

 

<Window x:Class="simple_sample_App.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">
    <Grid>
        <Label Content="Name" HorizontalAlignment="Left" Margin="157,56,0,0" VerticalAlignment="Top"/>
        <Label Content="Location" HorizontalAlignment="Left" Margin="157,87,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtName" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="233,58,0,0" Width="120"/>
        <TextBox x:Name="txtLocation" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="233,89,0,0" Width="120"/>
        <Button x:Name="btnSubmit" Content="Submit" HorizontalAlignment="Left" Margin="233,126,0,0" VerticalAlignment="Top" Width="75" Click="btnSubmit_Click"/>
        <Label Content="Name" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="157,211,0,0"/>
        <Label Content="Location" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="157,242,0,0"/>
        <Label x:Name="lblName" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="233,211,0,0"/>
        <Label x:Name="lblLocation" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="233,242,0,0"/>
        <Label Content="Result :" HorizontalAlignment="Left" Margin="157,180,0,0" VerticalAlignment="Top"/>
        <Label Content="WPF Sample Application" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="157,10,0,0" FontFamily="Arial Black" FontSize="16"/>

    </Grid>
</Window>

XAML.CS Code :
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 simple_sample_App
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            lblName.Content = txtName.Text;
            lblLocation.Content = txtLocation.Text;
        }
    }
}
 


OutPut :

 


CRUD operations using AngularJS in Asp.Net MVC

In this article I will show you how to perform CRUD operations in Asp.Net MVC5 using AngularJS. Add AngularJS using the Manage Nuget Pack...