-->

Monday, August 31, 2015

how to get data from sql database to gridview by coding

Introduction
In this article i will show you how to get data from sql server to GridView by coding. Example of binding gridview with SQL Server database.
The C# GridView Control
The C# GridView control is a Data bound control that displays the values of a data source in the form of a table . In this table , each column represents a field and each row represents a record . The C# GridView control exists within the System.Web.UI.Controls namespace . When you drag and drop the GridView control on the designer page , the following syntax is added to the source view of the page.

<asp:gridview id="GridView1" runat="server"> </asp:gridview>

ASP.NET reduce lots of code in binding process, provide binding controls such as SqlDataSource for connecting database. The GridView data control has a built-in capability of sorting ,paging , updating and deleting data. You can also set the column fields through the AutoGenerate property to indicate whether bound fields are automatically created for each field in the data source.
In the below mentioned example, step 1 is define for add gridview in the webpage. Now, come to second step create columns in the gridview, which is depend on user choice. Under the <Columns> tag, create TemplateFields, which is used for design first column, according to step 3.


STEP-1 : Drag Gridview from Toolbox and Drop on design window.

    <form id="form1" runat="server">
    <div>  
        <asp:GridView ID="GridView1" runat="server">        
        </asp:GridView>  
    </div>
    </form>

STEP-2 : Create columns inside Gidview

<asp:GridView ID="GridView1" runat="server">
            <Columns >
            </Columns>        
        </asp:GridView>

STEP-3 : Create first column using TemplateField

  <Columns >
<asp:TemplateField>
</asp:TemplateField>
            </Columns>

STEP-4 : Create ItemTemplate inside <asp:TemplateField>
STEP-5 :  Also bind with table Attribute/Field

<asp:TemplateField>
   <ItemTemplate>
    <%# Eval("EmployeeId") %>
        </ItemTemplate>
</asp:TemplateField>

STEP-6:
Make StoredProcedure for retrieving data from database table.

CREATE PROCEDURE [dbo].[getdata]

AS
SELECT * from [Table]

RETURN 0



STEP-7 : Create code for binding data

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class binggrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page .IsPostBack)
        {
            getdataload();

        }

    }

    private void getdataload()
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            con.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "getdata";
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                using (DataSet ds = new DataSet())
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        GridView1.DataSource = ds;
                        GridView1.DataBind();

                    }

                }  
         
            }
       
        }

    }
}

Output of the program
C# gridview bind in asp.net

Note : Some Extra Column appear in gridview like EmployeeId, name
if you want to remove these column add attribute inside gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">

Main Output of the program are:



C# gridview bind in asp.net
Here we are using single Template field. If you want to make more column then you must take more than one template field. Each template specifies column of the GridView.


Top related post

Friday, August 28, 2015

How to insert data into ms access database table in wpf

Introduction
In previous article i explained how to create connection with MS-Access database; How to bind DataGrid with access database. In this article i will teach you how to insert item into Access database using WPF. Lets take a simple example of inserting item into access database in WPF.



Description
First of all create connection with the access database which is already done in previous article. Now, you will do some changes in code of  data grid binding. Like replace DQL query with DML query.

Like :
cmd.CommandText = "Insert into [tablename](columnName)Values(Data)";

Also do not required to execute the query, use ExecuteNonQuery() method to insert data by the input box.

Source Code:

<Window x:Class="WpfApplication1.insertingitem"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="insertingitem" Height="300" Width="300">
    <Grid>
        <Label Content="Enter Name" HorizontalAlignment="Left" Margin="10,31,0,0" VerticalAlignment="Top"/>
        <TextBox Name="nametxt" HorizontalAlignment="Left" Height="23" Margin="98,31,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
        <Button Click="Button_Click" Content="Save" HorizontalAlignment="Left" Margin="33,80,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>

Code Behind

using System.Windows;
using System.Data.OleDb;
using System.Configuration;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for insertingitem.xaml
    /// </summary>
    public partial class insertingitem : Window
    {
        public insertingitem()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString=ConfigurationManager.ConnectionStrings["Connection"].ToString();
            con.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "insert into [Employee](Name1)Values(@nm)";
            cmd.Parameters.AddWithValue("@nm", nametxt.Text);
            cmd.Connection = con;
            int a = cmd.ExecuteNonQuery();
            if (a>0)
            {
                MessageBox.Show("Item Inserted");
                
            }


        }
    }
}

Code generate the following output


Thursday, August 27, 2015

How to insert data into MS-Access database table in asp.net c#

Introduction
In previous article i explained how to create connection with MS-Access database; How to bind DataGrid with access database. In this article i will teach you how to insert item into Access database using ASP.NET C#. Lets take a simple example of inserting item into access database in asp.net.


Description
First of all create connection with the access database which is already done in previous article. Now, you will do some changes in code of  GridView binding. Like replace DQL query with DML query.

Like :
cmd.CommandText = "Insert into [tablename](columnName)Values(Data)";

Also do not required to execute the query, use ExecuteNonQuery() method to insert data by the input box.

Source Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        Enter Name :
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Enter Data into Excess" />
 
    </div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;

public partial class Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString=ConfigurationManager.ConnectionStrings["Connection"].ToString();
        con.Open();
        OleDbCommand cmd=new OleDbCommand();
        cmd.CommandText = "insert into [Employee](Name1)values(@nm)";
        cmd.Parameters.AddWithValue("@nm", TextBox1.Text);
        cmd.Connection = con;
        int a = cmd.ExecuteNonQuery();
        if (a>0)
        {
            Label1.Text = "Inserted";
        }
    }
}

Code generate the following output

How to insert data into MS-Access database table in asp.net c#

Sunday, August 23, 2015

How to insert item into MS-access database using windows form c#

Introduction
In previous article i explained how to create connection with MS-Access database; How to bind DataGrid with access database. In this article i will teach you how to insert item into Access database using windows form. Lets take a simple example of inserting item into access database.


Description
First of all create connection with the access database which is already done in previous article. Now, you will do some changes in code of  datagrid binding. Like replace DQL query with DML query.

Like :
cmd.CommandText = "Insert into [tablename](columnName)Values(Data)";

Also do not required to execute the query, use ExecuteNonQuery() method to insert data by the input box.

using System;
using System.Configuration;
using System.Data.OleDb;
using System.Windows.Forms;

namespace ConnectAccessDatabase
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString=ConfigurationManager.ConnectionStrings["Connection"].ToString();
            con.Open();

            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "insert into [Employee](Name1)values(@nm)";
            cmd.Parameters.AddWithValue("@nm", textBox1.Text);
            cmd.Connection = con;
            int a = cmd.ExecuteNonQuery();
            if(a>0)
            {
                MessageBox.Show("Inserted");
            }
        }
    }
}

Code generate the following output

How to insert item into MS-access database using windows form c#



How to bind DataGrid with MS-Access database in WPF

Introduction

In Previous article we have already learn that how to connect MS-Access with windows form. Today we will take a example of binding DataGrid with MS-Access in WPF c#. Also learn how to create connection using OleDbConnection class which is exist in System.Data.OleDb namespace.

Description:

First to read, How to create Database table in Ms-Access and how to create connection with MS-Access using c#. Now, after creating the database you can create the command using OleDbCommand class which is also available in System.Data.OleDb namespace. Use OleDbDataReader Class which is used to read the database table in forward only mode. Now, Bind the DataGridView with Data Reader. Check the below mentioned code as well as video:

Database table


How to Bind Gridview with Access database in ASP.NET C#


Source code : 

<Grid>
        <DataGrid Name="grid1" HorizontalAlignment="Left" Margin="116,72,0,0" VerticalAlignment="Top" Height="201" Width="215"/>


    </Grid>

Connection String in web.config file
<connectionStrings>

    <add name ="Connection" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Dbase.accdb;Persist Security Info=False;"/>

  </connectionStrings>
Code Behind 

using System;
using System.Collections.Generic;
using System.Configuration;
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 WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            loadgrid();
        }

        private void loadgrid()
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString=ConfigurationManager.ConnectionStrings["Connection"].ToString();
            con.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "select * from [Employee]";
            cmd.Connection = con;
            OleDbDataReader rd = cmd.ExecuteReader();
            grid1.ItemsSource = rd;
        }
    }
}

Saturday, August 22, 2015

How to Bind Gridview with Access database in ASP.NET C#

Introduction

In Previous article we have already learn that how to connect MS-Access with windows form. Today we will take a example of binding Gridview with MS-Access in asp.net c#. Also learn how to create connection using OleDbConnection class which is exist in System.Data.OleDb namespace.

Description:

First to read, How to create Database table in Ms-Access and how to create connection with MS-Access using c#. Now, after creating the database you can create the command using OleDbCommand class which is also available in System.Data.OleDb namespace. Use OleDbDataReader Class which is used to read the database table in forward only mode. Now, Bind the DataGridView with Data Reader. Check the below mentioned code as well as video:


Database table


How to Bind Gridview with Access database in ASP.NET C#


Source code : 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </form>
</body>
</html>

Connection String in web.config file
<connectionStrings>

    <add name ="Connection" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Dbase.accdb;Persist Security Info=False;"/>

  </connectionStrings>
Code Behind 

using System;
using System.Configuration;
using System.Data.OleDb;
using System.Web.UI;

public partial class Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            loadgrid();
        }
    }

    private void loadgrid()
    {
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
        con.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "Select * from [Employee]";
        cmd.Connection = con;
        OleDbDataReader rd = cmd.ExecuteReader();
        GridView1.DataSource = rd;
        GridView1.DataBind();
    }
}

Thursday, August 20, 2015

How to Bind DataGridView with MS-Access in windows form c#

Introduction

In Previous article we have already learn that how to connect MS-Access with windows form. Today we will take a example of binding DataGridView with MS-Access. Also learn how to create connection using OleDbConnection class which is exist in System.Data.OleDb namespace.

Description:

First to read, How to create Database table in Ms-Access and how to create connection with MS-Access using c#.



Now, after creating the database you can create the command using OleDbCommand class which is also available in System.Data.OleDb namespace. Use DataSet Class which is used to load the database table to in-memory. Now, Bind the DataGridView with DataSet Table. Check the below mentioned code as well as video:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;

namespace ConnectAccessDatabase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            loadgrid();
        }

        private void loadgrid()
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
            con.Open();
            MessageBox.Show("Connection Sucess");
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "Select * from [Employee]";
            cmd.Connection = con;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];

        }
    }
}

How to connect Microsoft Access Database to Windows Form c#

Introduction
In this article we will learn how to connect access database to windows form application. Get the MS Access data in windows form application. Access is a database system or you can its a tool of MS-Office. Through this we can store data permanently in it. If you want to store data in it by the help of windows application then you should connect it with the application.

Description
If you want to connect the database with the windows form application then you must to install the MS- Access in the computer. Also must to install connection drivers properly. Now, follow the instruction to connect ms-access database to windows form c#.

  1. First to prepare a database file in MS-Access.
  2. Add a windows form project Using Visual Studio 2013 or earlier.
  3. Add a Method just after Initializing Component. Like 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;

namespace ConnectAccessDatabase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            loadgrid();
        }

        private void loadgrid()
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
            con.Open();
            MessageBox.Show("Connection Sucess");
  

        }
    }
}

Monday, August 17, 2015

Get the page name example in asp.net c#

In this article i will learn how to get the page name in asp.net. If you are using visual studio 2005 to 2010 then url show page name with extension. But if you are using vs2012 to vs2015 then do not show. Lets check the example of  get page name from page url.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get the page name program in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Get Page Name" OnClick="Button1_Click" />
        <br />
        <br />
        <asp:Label ID="lblresult" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>
Code Behind
using System;
using System.IO;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string pagename = Path.GetFileName(Request.Path);
        lblresult.Text = pagename;
    }
}

Code generate the following output


Get the page name in asp.net c#

Sunday, August 16, 2015

Random password generator software free download

Introduction

This software is used to generate random password. Free download random password generator software. Through this password generator you can generator long string password like this

Password: Asdfrte,./7864@3k#
Example of long/strong/good password. If you want to know about this software that how it work?
First to run the executable file and open the software in windows environment and put some number like 15. After entered you get 15 character long string password.
How to design the software:
First to create a new project in visual studio , File-->New-->Project-->Windows form (c# Language). Design a form with one label, one TextBox, one button control. When you press the button then you get long string password. So put this code on Button click.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RandomPasswordGenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string randomstring = string.Empty;
            char[] array = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM[];,.?@#$%^&*()".ToCharArray();
            Random r1 = new Random();
            int getnumber = Convert.ToInt32(textBox1.Text);
            for(int i=0;i<getnumber;i++)
            {
                int point = r1.Next(1, array.Length);
                if (!randomstring.Contains(array.GetValue(point).ToString()))
                    randomstring += array.GetValue(point);
                else
                    i--;
            }
            lblresult.Text = randomstring;
        }
    }
}



Code Generate the following output
Random password generator software download

Saturday, August 15, 2015

ZoomIn and ZoomOut Text Example using JQuery

Introduction: 

Here I will explain example of Text ZoomIn or ZoomOut example using JQuery or increase or decrease font size of blog/website dynamically on button click using jQuery in asp.net.

Description: 
 
In previous articles I explained ajax password strength validator and checker. This all things are related to Jquery. To read more article about JQuery which is implemented in asp.net.

To implement this we need to write the following code in aspx page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            var getdiv = $('#test');

            //get zoomin button

            $('#zoomin').click(function () {

                var presentsize = getdiv.css('fontSize');
                var latestsize = parseInt(presentsize.replace("px", "")) + 1;
                $(getdiv).css("fontSize",latestsize + "px");

            });

            //get zoomout button

            $('#zoomout').click(function () {

                var presentsize = getdiv.css('fontSize');
                var latestsize = parseInt(presentsize.replace("px", "")) - 1;
                $(getdiv).css("fontSize", latestsize + "px");

            });

        })

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="test">
        Welcome to my blog: http://dotprogramming.blogspot.com
 
    </div>
        <input id="zoomin" type="button" value="+" />
        <input id="zoomout" type="button" value="-" />
    </form>
</body>
</html>

Code Generate the following output:

ZoomIn and ZoomOut Text Example using JQuery

Thursday, August 13, 2015

Ajax Password Strength and password indicator example in asp.net

Introduction

In this article i will learn about password strength control of ajax. Example of password indicator of ajax in asp.net. This example cover all such things which is related to password strength.

Description

i already explained about Always visible control example in ajax. Example of calendar extender using ajax. Example of Numeric UpDown Extender example using ajax. Example of Resizable control in asp.net. Slider Extender example in AJAX.


Source code :

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .very{
background-color:gray;
color:white;
        }
.weak{
    background-color:Red;
color:white;

}
.average{
    background-color:blue;
color:white;
}
.good{
    background-color:orange;
    color:green;
}
.excellent
{
    background-color:green;
    color:black;
}
        .barline
        {
            border-style:solid;
            border-width:2px;
            width:190px;
            padding:2px;

        }
  </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <div>
        Password:
        <asp:TextBox TextMode="Password" ID="TextBox1" runat="server" Height="23px" Width="227px"></asp:TextBox>
&nbsp;</div>
        <p>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <cc1:PasswordStrength PreferredPasswordLength="8" TargetControlID="TextBox1" ID="pwdstrength" runat="server" StrengthIndicatorType="Text" HelpStatusLabelID="Label1" MinimumNumericCharacters="1" MinimumSymbolCharacters="1" TextStrengthDescriptions="very;weak;average;good;excellent" />
        </p>
        <p>
            &nbsp;</p>
        <p>
            PassWord Indicator :
        <asp:TextBox TextMode="Password" ID="TextBox2" runat="server" Height="23px" Width="227px"></asp:TextBox>
            <cc1:PasswordStrength TextStrengthDescriptionStyles="very;weak;average;good;excellent" BarBorderCssClass="barline" PreferredPasswordLength="8" TargetControlID="TextBox2" ID="TextBox2_PasswordStrength" runat="server" StrengthIndicatorType="BarIndicator" HelpStatusLabelID="Label2" MinimumNumericCharacters="1" MinimumSymbolCharacters="1" TextStrengthDescriptions="very;weak;average;good;excellent" />
       
        </p>
        <p>
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        </p>
       
    </form>
</body>
</html>
In this example TextStrengthDescriptionStyles cover all css classes which is covered in the example. StrengthIndicatorType define the appearance of the validator.

Code generate the following outout

Ajax Password Strength and password indicator example in asp.net



Tuesday, August 11, 2015

How to add StaticResource and DynamicResource style in WPF

Introduction

In this article i will explain about style in wpf, How to use staticResources in wpf. Through the windows.Resources tag i will explain how to use style in xaml. Also use the DynamicResources in app.xaml file. Example of DynamicResourcs style in app.xaml file. 

Description

If you want to do some changes in formatting of your page or control then add some attribute like Background Foreground etc in xaml tag.But if you want to do same changes with 1000 of controls then what to do. There are two options first one is changes in all tags and second one is changes in one place but effect appear in all controls. Now, this example i will use both methods for you.



This video cover:
 How to add TextBlock in wpf window, How to add inline style in wpf . How to add foreground and background of TextBlock in wpf. Example of TextBlock control in wpf. How to change color of TextBlock in wpf. How to change foreground and background color of button control. How to use window.Resources in wpf. Example of window.Resources. How to create style in window.Resources. Example of style tag in xaml. Example of setter property of style tag. How to apply style on controls in wpf.

DynamicResources in WPF



This video cover:
How to use Application.Resources in wpf, example of Application.Resources. How to add style in app.xaml file. Example of TargetType property of style tag in wpf. How to use setter property of style in application.Resources tag. How to use application.Resources code on wpf controls.  

Sunday, August 9, 2015

How to bind Dropdownlist from all countries name in asp.net c#

In this article i will show you how to bind the dropdownlist to countries name. You can say load dropdownlist from countries names like United State, United Kingdom etc in asp.net c#. By the System.Globalization namespace you can bind dropdownlist to countries name in asp.net c#.
In previous article i explained some topics on dropdownlist:


Source code of this article:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BindCountries.aspx.cs" Inherits="BindCountries" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="28px" Width="118px"></asp:DropDownList>
    </div>
    </form>
</body>
</html>

Code Behind:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BindCountries : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            List<string> countries = new List<string>();
            CultureInfo[] culture = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            foreach (CultureInfo item in culture)
            {
                RegionInfo region = new RegionInfo(item.LCID);
                if(!(countries.Contains(region.EnglishName)))
                {
                    countries.Add(region.EnglishName);
                }
            }
            countries.Sort();
            DropDownList1.DataSource = countries;
            DropDownList1.DataBind();
        }
    }
}

Above mentioned code define the following:
I have a list with string type, bind this English name of all countries name using System.Globalization namespace. First to bind array with the all cultures, now from all cultures you can get specific region.

Code generate the following output

How to bind Dropdownlist from all countries name in asp.net c#

© Copyright 2013 Computer Programming | All Right Reserved