-->

Wednesday, September 18, 2013

How to use DataTable in ASP.NET


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
        <asp:Button ID="Button1" runat="server" Height="37px" onclick="Button1_Click"
            Text="Bind Gridview" Width="130px" />
        <br />
  
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>
</html>
Codebehind

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;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      
        using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from [mytab]", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
                       GridView1.DataSource = dt;
            GridView1.DataBind();
          

        }
    }
}


Output
How to use DataTable in ASP.NET

How to Place Items in TabControl: WPF

In our previous post, we have learnt about tab control and place some tabs into that control. In this post we will place a container, means we will place a simple entry form into a tab item. As we all know the entry form can contain name, age, address or some more basic information about a person.

So I am taking only name and age here as for example. Just write the following code in our XAML code window:
<TabControl Name="tabControl" Margin="5">
<TabItem Header="Entry Form Tab">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Label Content="Name"/>
<Label Content="Age" Grid.Row="1"/>

<TextBox Margin="2" Grid.Column="1"></TextBox>
<TextBox Margin="2" Grid.Column="1" Grid.Row="1"></TextBox>
</Grid>
</TabItem>
</TabControl>


Just run the code and a tab control is placed on our window with an entry form. The form will look like the following image:

How to customize tab control in WPF and XAML

So we have simply placed an entry form into tab item. We can set anything as the content property of tab item, just as above code.

In the above code, i have modify only a single tab item for example. If you need more tabs then you can modify as many tabs as you want to.

How to Create Tabs using TabControl: WPF

When there is not much space on our screen then programmer uses a tab control. Tab control is used to organize multiple tabs on WPF window, which can performs individual function decided by the programmer. It can contains collection of objects of any type i.e. string, image or a container.

Through XAML, we can place a tab control easily using the element Tab Control. To place some tabs into tab control, we have to use TabItem elements. Write the following code in your XAML:
<TabControl Name="tabControl" Height="400" Width="300" Margin="5">
<TabItem Header="Tab 1" Width="70"></TabItem>
<TabItem Header="Tab 2" Width="70"></TabItem>
<TabItem Header="Tab 3" Width="70"></TabItem>
</TabControl>

Now run the code and check out the tab control. It have three tabs with their own header as described above.

How to add a Tabcontrol in XAML and WPF


The same tab control can also be generated through the following code in C# code behind file:
TabControl tabcontrol = new TabControl();
tabcontrol.Name = "tabControl";
tabcontrol.Height = 400;
tabcontrol.Width = 300;
tabcontrol.Margin = new Thickness(5);

TabItem tabItem1 = new TabItem();
tabItem1.Header = "Tab 1";
tabcontrol.Items.Add(tabItem1);

TabItem tabItem2 = new TabItem();
tabItem2.Header = "Tab 2";
tabcontrol.Items.Add(tabItem2);

TabItem tabItem3 = new TabItem();
tabItem3.Header = "Tab 2";
tabcontrol.Items.Add(tabItem3);

this.Content = tabcontrol;

In next article, we will place some items/containers in these tab items. The containers could have other items/containers.

How to bind CheckBoxList using SqlDataSource in ASP.NET

SqlDataSource 

The SqlDataSource control is a DataSource control that allows a server control, Such as the GridView control, to access Data located in a relational database, such as Microsoft SQL Server and Oracle.
This control also generates a connection string to interact with data in an ASP.NET page. These connection strings are generated through the Configure Data Source wizard. A Connection String contains informattion about the server, database , and security you are working with in your application , for example
Data Source= .\SqlExpress; Initial Catalog =Database name ; Integrated Security = True
The SqlDataSource control automatically opens a database connection , which is provided in the connection string, executes the SQL queries , and then close the connection. When you add a SqlDataSource control to an ASP.NET page , the following code appears in the HTML code of the page.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"> </asp:SqlDataSource>

CheckBoxList 

You can use a CheckBoxList control to display a number of check boxes at once as a column of check boxes . The CheckBoxList control exists within the System.Web.UI.WebControls namespace. This control is often useful when you want to bind the data from a data source to checkboxes . The CheckBoxList control creates multiselection checkbox groups at runtime by binding these control to a data source.

Lets take an example for binding CheckBoxList using SqlDataSource
Step-1: Drop CheckBoxList control to design window 
Step-2: Choose DataSource from showSmart tag ..... 
Step-3:  Same as GridView Binding

Complete Source code




<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
        <asp:CheckBoxList ID="CheckBoxList1" runat="server"
            DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="id">
        </asp:CheckBoxList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT * FROM [mytab]"></asp:SqlDataSource>
  
    </div>
    </form>
</body>
</html>

Output
How to bind CheckBoxList using SqlDataSource in ASP.NET

Tuesday, September 17, 2013

How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET

If you bind your application with database in traditional ASP program then you should use RecordSet and write lots of code for binding . But in ASP.NET Microsoft reduce lots of code for binding application. In ASP.NET you can use SqlDataSource for connecting front-end to back-end.

lets take an example , Add insert , update and delete functionality through SqlDataSource.
Step-1: Drop SqlDataSource control to the design window.
Step-2: Select Configure Data Source using Show Smart tag.
Step-3: Select Connection from DropdownList , expand connection string
now your connection string are
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True
Click Next to continue
Step-4: Select Advanced.. Button for adding insert,edit and delete functionality.
How to Insert,Edit,Delete Record using SqlDataSource
If your database table contain one primary key then you can select "Generate INSERT, UPDATE, AND DELETE STATEMENT"  And click to Ok button.

How to Insert,Edit,Delete Record using SqlDataSource
Step-5: After finishing process , Drop one DetailsView Control to the page. and Choose SqlDataSource1 for connecting database using ShowSmart tag.
How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET
Step-6: Select all given CheckBox
How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET

Complete code
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [footerex] WHERE [Id] = @Id" InsertCommand="INSERT INTO [footerex] ([namet], [Income]) VALUES (@namet, @Income)" SelectCommand="SELECT * FROM [footerex]" UpdateCommand="UPDATE [footerex] SET [namet] = @namet, [Income] = @Income WHERE [Id] = @Id">
            <DeleteParameters>
                <asp:Parameter Name="Id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="namet" Type="String" />
                <asp:Parameter Name="Income" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="namet" Type="String" />
                <asp:Parameter Name="Income" Type="Int32" />
                <asp:Parameter Name="Id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
 
    </div>
        <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
            <Fields>
                <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="namet" HeaderText="namet" SortExpression="namet" />
                <asp:BoundField DataField="Income" HeaderText="Income" SortExpression="Income" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
    </form>
</body>
</html>


Output
How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET

How to Insert,Edit,Delete Record using SqlDataSource in ASP.NET

Monday, September 16, 2013

How to Bind ComboBox with DataTable: WPF

DataTable is an object of ADO.NET library, which is used to store temporary data in tabular form in memory. When we are creating data table in code part, then we have to add some columns and have to add some rows.

We have learnt about the combo box introduction and its binding with the list of string in our earlier posts. In this article we will bind this combo box to a data table. Just add a combo box and write some properties as in below code:
<ComboBox Name="cmbBox" Height="25" Width="200"
ItemsSource="{Binding}"></ComboBox>

Now in code part, create an object of data table and add three columns i.e. Name, Age and Address, like in below c# code:
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");
dt.Columns.Add("City");

After adding columns, now its time to add some rows and insert some records in that. So write the below c# code in code part to add three rows:
DataRow dr = dt.NewRow();
dr["Name"] = "Jacob";
dr["Age"] = 25;
dr["City"] = "France";
dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();
dr1["Name"] = "Julia Martin";
dr1["Age"] = 26;
dr1["City"] = "France";
dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();
dr2["Name"] = "Brandon";
dr2["Age"] = 24;
dr2["City"] = "London";
dt.Rows.Add(dr2);

We have successfully created a data table with three columns and add three rows with some valid data. To bind this data table to above combo box, we have to write these two lines in c# code part:

cmbBox.DataContext = dt;
cmbBox.DisplayMemberPath = dt.Columns[0].ToString();

The first line will assign the data context of combo box to the data table, while the second line will set the column name to be displayed in combo box. Run the project the check the combo box:

How to bind combo box with data table in WPF C#, XAML

How to Customize Status Bar in WPF

In earlier post, we have learnt about placing a status bar and add some items on that. WPF provides some advanced options with status bar i.e. we can add some more items or even containers in status bar. In this article we will add a stack panel, image and some more items in a status bar.

Just write the following code in our XAML code window:
<StatusBar Name="statusBar" VerticalAlignment="Bottom">
<StackPanel Orientation="Horizontal">
<StackPanel>
<StatusBarItem Margin="5" >Status 1</StatusBarItem>
<StatusBarItem Margin="5" >Status 2</StatusBarItem>
<StatusBarItem Margin="5" >Status 3</StatusBarItem>
</StackPanel>
<Image Margin="5" Source="wall.jpg" Width="100" Height="30"></Image>
<ProgressBar Width="150" Height="20" Name="progressBar" Value="45"
HorizontalAlignment="Right" Margin="5"></ProgressBar>
</StackPanel>
</StatusBar>

Run the project and it will show a window with three strings in a stack panel, an image and a progress bar. The first stack panel also contain a nested stack panel. These items are added just for example, not for any purpose.

Customize status bar in WPF


Just like the above code, we can place any control in the status bar, according to our requirements.
© Copyright 2013 Computer Programming | All Right Reserved