-->

Friday, September 25, 2015

Bind DataList from database in asp.net c#

Introduction

In this article i will show you how to bind dataList from database table. Also i will give an example of it. In this example i will set the orientation property of items also set columns which is appear on the web page.

The DataList Control

The DataList control is a Data bound control that display data by using templates. These templates define controls and HTML elements that should be displayed for an item. The DataList control exists within the System.Web.UI.WebControl namespace.
Now , lets take an example.

How to bind DataList Control using the SqlDataSource

Follow some steps for binding Datalist to SqlDataSource . Steps same as Previous post. Basically SqlDataSource is used to connect front-end to  back-end using connection string . Connection string  saved in configuration file.
Now , After establishing connection you have to use property builder through ShowSmart tag.

DataList Property builder
Select Property Builder link , Now after selected a new window will open 


dataList Properties

Run You Application

<%@ 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:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="SqlDataSource1" Height="285px" RepeatColumns="3" RepeatDirection="Horizontal" Width="134px">
            <ItemTemplate>
                Id:
                <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
                <br />
                namet:
                <asp:Label ID="nametLabel" runat="server" Text='<%# Eval("namet") %>' />
                <br />
                Income:
                <asp:Label ID="IncomeLabel" runat="server" Text='<%# Eval("Income") %>' />
                <br />
<br />
            </ItemTemplate>
        </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [footerex]"></asp:SqlDataSource>
 
    </div>
    </form>
</body>
</html>

Output
How to use DataList Control in ASP.NET

Thursday, September 24, 2015

Display sum of Columns total in GridView Footer in ASP.Net using C#

Introduction
In this article i will show you how to show total of all rows of GridView column. I will give you an example of this problem.Suppose you have a one numeric type columnn in your data table and you want to add all column values.Also display sum of column in footerRow of the GridView. If we think about algorithm for this type of problem. Algorithm say that first we take a Gridview with Two template  field , One template field have one item template and one footer template such as



  <asp:TemplateField HeaderText ="Name">
                <ItemTemplate >

                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("namet") %>'>

                    </asp:Label>
               


                </ItemTemplate>
                <FooterTemplate >
                    Total:
                 
                </FooterTemplate>
            </asp:TemplateField>

Another template field have same field which is take above such as

<asp:TemplateField HeaderText ="Income">
                <ItemTemplate>
               <asp:Label ID="incomelbl" runat="server" Text='<%# Eval("Income") %>' />

                </ItemTemplate>
                <FooterTemplate >
                   <asp:Label ID="footerlbl" runat="server" Text="" />
                 
                </FooterTemplate>


            </asp:TemplateField>


This whole part cover designing


In codebehind model first we would bind GridView on page Load method. After that handle RowDataBound event . In RowDataBound event  first check Row type is DataRow.

After that find income label in Gridview using findcontrol method.

 if (e.Row.RowType ==DataControlRowType .DataRow)
        {
            var incomsal = e.Row.FindControl("incomelbl") as Label;
            if (incomsal != null)
            {
                total += int.Parse(incomsal.Text);
            }
       
        }

Here Total is a integer variable.
Now Show Total income to footer label then you must find control from the footer row on Page_Load method

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page .IsPostBack)
        {
           binddata();
           var footlbl = GridView1.FooterRow.FindControl("footerlbl") as Label;
           if (footlbl != null)
           {
               footlbl.Text  = total.ToString();
           }
        }
   
    }

    After founded , Total value assign to footer label
    Run you program.

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

<!DOCTYPE html>

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

            <asp:TemplateField HeaderText ="Name">
                <ItemTemplate >

                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("namet") %>'>

                    </asp:Label>
               


                </ItemTemplate>
                <FooterTemplate >
                    Total:
                 
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText ="Income">
                <ItemTemplate>
               <asp:Label ID="incomelbl" runat="server" Text='<%# Eval("Income") %>' />

                </ItemTemplate>
                <FooterTemplate >
                   <asp:Label ID="footerlbl" runat="server" Text="" />
                 
                </FooterTemplate>


            </asp:TemplateField>


        </Columns>   



    </asp:GridView>
        <asp:Label ID="result" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

Codebehind
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class additem : System.Web.UI.Page
{
    int total = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page .IsPostBack)
        {
           binddata();
           var footlbl = GridView1.FooterRow.FindControl("footerlbl") as Label;
           if (footlbl != null)
           {
               footlbl.Text  = total.ToString();
           }
        }
     
    }
    public void binddata()
    {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        con.Open();
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.CommandText = "Select * from [footerex]";
        cmd.Connection = con;
        System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
        System.Data.DataSet ds = new System.Data.DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType ==DataControlRowType .DataRow)
        {
            var incomsal = e.Row.FindControl("incomelbl") as Label;
            if (incomsal != null)
            {
                total += int.Parse(incomsal.Text);
            }
         
        }

    }
}

Output
How to display total in GridView Footer in ASP.NET

Wednesday, September 23, 2015

Example to create tooltip using code in windows form c#

Introduction
In this article i will show you , how to generate tooltip on controls using c# code. By using tooltip we can put some hints about task. I will give you an example of ToolTip in windows form c#. In this article we will use ToolTip class with their SetToolTip method.


Description

Code

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 WindowsFormsApplication2
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
            settooltip();
        }

        private void settooltip()
        {
            ToolTip tip = new ToolTip();
            tip.SetToolTip(this.button1, "Default Push Button");
            tip.SetToolTip(this.textBox1, "Empty TextBox");
        }
    }
}

Change windows phone 8.1 app icon

Introduction
In this article i will show you how to change windows phone 8.1 app icon using manifest file. You can set your image file in place of default icon file. The default app icon file looking like cross square. I will provide you step by step guide to change the icon file or you can say image file.


  1. Double click on Package.appxmanifest file.
  2. Select Visual Assets tab.
  3. Add new image in Assets folder with 44x44 resolution.
  4. Change 44x44 resolution image file

Change windows phone 8.1 app icon

 Now, code generate the following icon

Change windows phone 8.1 app icon

Tuesday, September 22, 2015

Add column in WPF DataGrid using both xaml and code

Introduction
In this article i will show you how to add columns in DataGrid using xaml code and c# code. I will give you an example of add columns in WPF DataGrid. Datagrid is used to represent a tabular view of data in programming. It have multiple template designs to be customizable by the programmer as per the requirements. It supports some other features like sorting the data, dragging columns and sometimes editing when user needs to do.


To add a DataGrid, just drag-n-drop it from the toolbox. Now it is known as a tabular representation, write following code to add some columns (3 here):
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="First Column"/>
<DataGridTextColumn Header="Second Column"/>
<DataGridTextColumn Header="Third Column"/>
</DataGrid.Columns>
</DataGrid>

When we run this window then a Datagrid is shown with three columns like in below image:

How to add columns in DataGrid control in WPF

DataGrid provides columns sorting, reordering and sizing with some properties like CanUserReorderColumns, CanUserResizeColumns, CanUserSortColumns and etc. We can even perform grouping of data by adding a group description for the list to be bind.

To add a datagrid using code behind:
DataGrid dataGrid = new DataGrid();

DataGridTextColumn textColumn1 = new DataGridTextColumn();
textColumn1.Header = "First Column";
DataGridTextColumn textColumn2 = new DataGridTextColumn();
textColumn2.Header = "Second Column";
DataGridTextColumn textColumn3 = new DataGridTextColumn();
textColumn3.Header = "Third Column";

dataGrid.Columns.Add(textColumn1);
dataGrid.Columns.Add(textColumn2);
dataGrid.Columns.Add(textColumn3);

The above code snippet add a same datagrid with three columns as in above image. We will learn binding of datagrid in later articles.

Sunday, September 20, 2015

Example to change windows form shape using c#

Introduction
In this article i will show you, how to change the shape of the current form. The default form shape is square and i want to change it in ellipse form. Copy this code and paste into your code file.

Code

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 WindowsFormsApplication2
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            System.Drawing.Drawing2D.GraphicsPath shape1 = new System.Drawing.Drawing2D.GraphicsPath();
            shape1.AddEllipse(0, 0,this.Width, this.Height);
            this.Region = new System.Drawing.Region(shape1);
        }
    }
}
Code generate  the following output

Example to change windows form shape using c#

Saturday, September 19, 2015

Bind ASP.NET chart control from database table

Introduction
In this article i will show, how to bind chart control with database table in asp.net c#. I will give you example of it also provide video for implementation. A graph or chart diagram represents your set of data.If you want to show your company report then you must use chart control. There are different types of chart show different types of data such as



How to bind Chart Control in ASP.NET
Pie Chart
Using the pie-chart you can classify your data in graphical format. According to above diagram your movie are divided in different sections such as Action take 18% of whole movie as well as 11% take Horror.

Example of How to bind Chart Control using SqlDataSource
Step-1: Create Database Table in visual studio.

Chart Database table

.
Step-2: Bind connection with Database using SqlDataSource
<%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1" OnLoad="Chart1_Load">
            <series>
                <asp:Series ChartType="Pie" Name="Series1" XValueMember="movie_type" YValueMembers="percentage">
                </asp:Series>
            </series>
            <chartareas>
                <asp:ChartArea Name="ChartArea1">
                    <Area3DStyle Enable3D="True" />
                </asp:ChartArea>
            </chartareas>
        </asp:Chart>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [movie]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Output
How to bind Chart Control in ASP.NET

© Copyright 2013 Computer Programming | All Right Reserved