Search This Blog

Saturday, January 28, 2012

WPF - Windows Presentation Foundation.

WPF - Windows Presentation Foundation

                 WPF is used to create windows based application in asp.net framework. We can give impressive, attractive and effective GUI to the windows based application with the help of WPF.
                 WPF is the technology which comes with .NET Framework 3.0.


VERSIONS

                 Microsoft has released five major WPF versions: 
                 WPF 3.0 (Nov 2006), 
                 WPF 3.5 (Nov 2007), 
                 WPF 3.5sp1 (Aug 2008), 
                 WPF 4 (April 2010), and 
                 WPF 4.5 (August 2012).
 The latest Version of WPF is WPF 4.5 which released on Aug,12 by Microsoft.

What WPF can do

A WPF interface can combine images, text, 2D and 3D graphics, and more.

The Ability for Developers and Designers to Work Together

Vector Graphics System for Harnessing the Power of Graphics Hardware Acceleration


Intelligent Layout makes Design Easier


Advanced Styling Capabilities for Creating Beautiful Interfaces


What is XAML???


XAML stands for eXtensible Application Markup Language—pronounced “zammel.” 

With this new markup, UIs can be defined without the need to program, very similar to creating 

an HTML web page. 

Like Windows Forms, you build WPF forms using the interactive designer to drag and drop 

items on the UI and customize in the properties box. 

But unlike Windows Forms applications where the designer generates code in C#, VB.NET to 

create controls on the form, in a WPF application, the interactive designer generates a XAML script.

When you run the program, the XAML compiler converts the XAML into instances of objects 

using the .NET Framework. 

This is unlike most other markup languages, which are typically an interpreted language without

 such a direct tie to a backing type system.

GUI PAGE HAVING XAML FILE


XAML File

<Window x:Class="gridview.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="442" Width="583" Loaded="Window_Loaded">
    <Grid Height="402" Width="558">
        <Grid.RowDefinitions>
            <RowDefinition Height="404*" />
            <RowDefinition Height="38*" />
            <RowDefinition Height="6*" />
        </Grid.RowDefinitions>
        <ListView Name="Gridview1" ItemsSource="{Binding}" Margin="12,241,0,7" Grid.RowSpan="2">
            <ListView.View>
                <GridView>

                    <!--<GridViewColumn Header="Username">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock ></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>-->
                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=id}"></GridViewColumn>

                    <GridViewColumn Header="Username" DisplayMemberBinding="{Binding Path=username}"></GridViewColumn>

                    <GridViewColumn Header="Password" DisplayMemberBinding="{ Binding Path=password}"></GridViewColumn>
                    <GridViewColumn Header="Edit">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="Edit" Content="Edit" Click="Edit_Click"></Button>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView>
            </ListView.View>
        </ListView>
        <Label Content="Username" Height="28" HorizontalAlignment="Left" Margin="52,12,0,0" Name="label1" VerticalAlignment="Top" />
        <Label Content="Password" Height="28" HorizontalAlignment="Left" Margin="52,59,0,0" Name="label2" VerticalAlignment="Top" Width="63" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="149,14,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="149,0,0,278" Name="textBox2" VerticalAlignment="Bottom" Width="120" />
        <Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="149,111,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Button Content="Update" Height="34" HorizontalAlignment="Left" Margin="63,166,0,0" Name="update" VerticalAlignment="Top" Width="100" Click="update_Click" />
        <Button Content="Delete" Height="34" Margin="231,166,227,0" Name="delete" VerticalAlignment="Top" Click="delete_Click" />
    </Grid>
</Window>


.CS FILE


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.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
namespace gridview
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void fill_grid()
        {
            SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
            SqlDataAdapter sda1 = new SqlDataAdapter("select * from tbl_reg",con1);
            DataTable dt1 = new DataTable();
            sda1.Fill(dt1);
            //Gridview1.DataContext = dt1.DefaultView;
            Gridview1.DataContext = dt1.DefaultView;
                //Gridview1.DataContext = dt1.DefaultView;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            fill_grid();
        }

       
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
                SqlCommand cmd1 = new SqlCommand("insert into tbl_reg values('" + textBox1.Text + "','" + textBox2.Text + "')", con1);
                con1.Open();
                cmd1.ExecuteNonQuery();
                con1.Close();
                fill_grid();
                MessageBox.Show("Record is Successfully Inserted into the Database");
            }
            else
            {
                MessageBox.Show("Please Enter The Username and Password");
            }
        }

       

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (DataRowView)Gridview1.SelectedItem;
            string id1 = drv.Row[0].ToString();
            textBox1.Text = drv.Row[1].ToString();
            textBox2.Text = drv.Row[2].ToString();
             
        }

        private void update_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (DataRowView)Gridview1.SelectedItem;
            string id1 = drv.Row[0].ToString();
            SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
            SqlCommand cmd1 = new SqlCommand("update tbl_reg set username='" + textBox1.Text + "',password='" + textBox2.Text + "'  where id='" + id1 + "'", con1);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            MessageBox.Show("Record is Successfully Updated !!!");

        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (DataRowView)Gridview1.SelectedItem;
            string id1 = drv.Row[0].ToString();
            SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
            SqlCommand cmd1 = new SqlCommand("delete from tbl_reg where id='" + id1 + "'", con1);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            MessageBox.Show("Record Successfully Deleted from the Database");

        }

   
    }
}

Tuesday, January 24, 2012

what is delegates and why is it require???

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegates1//Used to pass method as argument
{

    public delegate string FirstDelegate(int x);

    class Program
    {
        static void Main(string[] args)
        {
            FirstDelegate d1 = new FirstDelegate(DelegateTest.StaticMethod);

            DelegateTest instance = new DelegateTest();
            instance.name = "My instance";
            FirstDelegate d2 = new FirstDelegate(instance.InstanceMethod);

            Console.WriteLine(d1(10)); // delegates d1 Writes out "Static method: 10"
            Console.WriteLine(d2(5));  // delegates d2 Writes out "My instance: 5"
            Console.ReadLine();
      
        }
    }
    class DelegateTest
    {
      public string name;

        public static string StaticMethod(int i)// It 's called by using class name
        {
            return string.Format("Static method: {0}", i);
        }

       public string InstanceMethod(int i)// it's called by using object of the class
        {
            return string.Format("{0}: {1}", name, i);
        }
    }
}

ienumrable interface

string[] name = {"kasdf","tops","tech"};

            IEnumerable<string> item = name
                                .Where(a => a.Length == 5)
                                .OrderBy(a => a)
                               .Select(a => a);
            foreach (var data in item)
            {
                DropDownList1.Items.Add(data);
            }
ienumarable is inter face used in enumrable class for create <type> of collection.


Thursday, January 19, 2012

Linq all operations

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
//    DataClassesDataContext linq = new DataClassesDataContext();
       DataClassesDataContext linq=new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fill();
            Fill_drop();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        tbl_reg t = new tbl_reg();
        t.username = txtname.Text;
        t.password = txtpwd.Text;
        //tbl_data t1 = new tbl_data();
        //t1.Uname = txtname.Text;
        //t1.Password = txtpwd.Text;
        linq.tbl_regs.InsertOnSubmit(t);
        linq.SubmitChanges();
        fill();
        Fill_drop();
        clear();
    }

    public void Fill_drop()
    {
        //var v = from l in linq.tbl_datas select l;

        //drplist.DataSource = v;
        //drplist.DataTextField = "Uname";
        //drplist.DataValueField = "Id";
        //drplist.DataBind();
        //drplist.Items.Insert(0, new ListItem("Select Name"));
    }

    public void clear()
    {
        txtname.Text = "";
        txtpwd.Text = "";
    }

    public void fill()
    {
        var str = from l in linq.tbl_regs select l;
        GridView1.DataSource = str;
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        TextBox txtuname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtuname");
        TextBox txtpassword = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpassword");
        tbl_reg t1 = linq.tbl_regs.Single(a => a.id == id);
        t1.username = txtuname.Text;
        t1.password = txtpassword.Text;
        linq.SubmitChanges();
        GridView1.EditIndex = -1;
        fill();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        fill();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        fill();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        tbl_reg t1 = linq.tbl_regs.Single(a => a.id == id);
        linq.tbl_regs.DeleteOnSubmit(t1);
        linq.SubmitChanges();
        fill();
}
    protected void  btn_select_Click(object sender, EventArgs e)
{
    var ds = from d in linq.tbl_regs

             orderby d.id descending
             select d;
              //  select new { d.id, d.password, d.username };
        GridView2.DataSource = ds;
        GridView2.DataBind();
       
}
protected void  btn_update_Click(object sender, EventArgs e)
{//Select from text box
        int n = int.Parse(txtpwd.Text.ToString());
        var ds =from d in linq.tbl_regs
                  where d.id == n
                  select d;
        GridView2.DataSource = ds;
        GridView2.DataBind();

}
}

String Manupulation operations




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LInq_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = new string[] { "one","two","three","four","five","six","Seven","Eight","Nine","Ten"};
            string[] ss = {"oo","one","two","three","eight" };
            var n = from str in s
                where str.Length<10
                orderby str.Length
                 orderby str
                 select str;
            //join query
                   //from str in s
                   //join str1 in ss
                   //on str equals str1
                   //select new { str, str1 };
            foreach (var item in n)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

Parallel Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace plinq123
{
    class Program
    {
        static void Main(string[] args)
        {
            Task.Factory.StartNew(() => NormalMethod());
            Console.ReadLine();
        }
        public static void NormalMethod()
        {
            int[] n = Enumerable.Range(1, 10000000).ToArray();
            Stopwatch s = new Stopwatch();
           
            s.Start();
            var count = (from v in n
                        where v % 11 == 0
                        orderby v descending
                        select v).ToArray();
            s.Stop();

            Console.WriteLine("The total numbers divisible by 2 are "+count.Count()+" in "+s.ElapsedMilliseconds+" Milliseconds");
                       
            s.Restart();           
            var count1 = (from v in n.AsParallel()
                        where v % 11 == 0
                        orderby v descending
                        select v).ToArray();
            s.Stop();

            Console.WriteLine("The total numbers divisible by 2 are " + count1.Count() + " in " + s.ElapsedMilliseconds + " Milliseconds");
       
        }
    }
}
Output: