Monday, 19 August 2013

How to insert data into Ms Access 2007 using WPF



Xaml.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:


AutoComplete Textbox in Windows Application C#.Net


To implement the AutoComplete Textbox in Windows form C#.net  you need to follow the below steps :

Step1 :
First you need to design a table in Ms Access 2007  to retrieve the records from database.

Step2:
On the
 File menu, click New Project. Select Windows Forms Application as your project type.
Design the Form using controls from Toolbox.

Step3:
Now open the Form.cs page and write the following source code

 Form.cs 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; 
using System.Data.OleDb;

namespace Autocomplete
{


    public partial class Autocomplete_TextBox : Form

    {

        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\App_Data\SampleDB.accdb; Jet OLEDB:Database Password=1234;Persist Security Info=False");
        AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();

        public AmirAds_MailSend()
        {
            InitializeComponent();
        }

         private void Autocomplete_TextBox_Load(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd = new OleDbCommand("Select distinct Email_Id from Sample_CreateClient order by Email_Id asc", con);
            cmd.CommandType = CommandType.Text;
            OleDbDataReader dr;
            dr= cmd.ExecuteReader();
            if (dr.HasRows == true)
            {
                while (dr.Read())
                    namesCollection.Add(dr["Email_Id"].ToString());
            }
            con.Close();

            txtToMail.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtToMail.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtToMail.AutoCompleteCustomSource = namesCollection;

        }
    }
}

  
Step4:
Now build the Solution and Debug it for the output.


Finding GeoLocation in Asp.net

To implement the Google Maps in Asp.Net with Demo you need to follow the below steps :

Step1 :
 
Create a new Asp.net website in Visual Studio and write the following html code in the design part of the Default.aspx page.


Default.aspx Code :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset=utf-8>
<meta name="viewport" content="width=620">
<title>Demo: Geolocation</title>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script>
        function success(position) {
            var s = document.querySelector('#status');

            if (s.className == 'success') {
                return;
            }

            s.innerHTML = "found you!";
            s.className = 'success';

            var mapcanvas = document.createElement('div');
            mapcanvas.id = 'mapcanvas';
            mapcanvas.style.height = '400px';
            mapcanvas.style.width = '560px';

            document.querySelector('article').appendChild(mapcanvas);

            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var myOptions = {
                zoom: 12,
                center: latlng,
                mapTypeControl: false,
                navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);

            var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                title: "You are here! (at least within a " + position.coords.accuracy + " meter radius)"
            });
        }

        function error(msg) {
            var s = document.querySelector('#status');
            s.innerHTML = typeof msg == 'string' ? msg : "failed";
            s.className = 'fail';
        }

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(success, error);
        } else {
            error('not supported');
        }

</script>
</head>
<body>
<section id="wrapper">
    <header>
      <h1>geolocation</h1>
    </header>
<meta name="viewport" content="width=620" />
    <article>
      <p>Finding your location: <span id="status">checking....</span></p>
    </article>
</section>
</body>
</html>

OutPut : 
Step2 : Now build the solution and debug it for output.