Kamis, 31 Oktober 2013

How to Add Commands with Binding in DataGridView: Windows Forms

After binding records with DataGridView, there may be some options to edit or to remove those records. To do these simple task we can provide either a button, which gets the selected record and perform operation, or we can provide the button with each particular record.

We will discuss here how to add a button with particular record with the following simple steps. Drag-n-drop a datagridview on the form and a task pop-up menu will automatically open, click on Add Columns link button to add a new column. It will show following Add Column window.

How to Add Commands with Binding in DataGridView: Windows Forms

By default Unbound column radio button have been checked, if not check it and use the following values in respective fields:
  • Type: DataGridViewButtonColumn
  • Header Text: Command (Changeable)
Sure the Visible check box is checked and click on Add button to add this column. Now you have successfully added a command button with empty text as shown in below image:

How to Add Commands with Binding in DataGridView: Windows Forms

To write text on the button generate Cell_Formatting event of dataGridview and set the value of first column as I do in the following c# code:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[0].Value = "Remove";
}

When you run the project now, it will show “remove” on the button. To check if this code will works for multiple records or not, add some records in data gridview like How to add Records in Datagridview. Run the project now and it will show the Remove button with each record as shown in the following image:

How to Add Commands with Binding in DataGridView: Windows Forms

In the next article I will remove the particular record with this command button.

How to Bind ComboBox with List of Items: C#

A list of items is used to store some temporary records, used in the program and do some operations like sorting, grouping and etc. In the previous post we have created a list and bind that list to the datagridview, using its DataSource property.

A combo box also have DataSource property to bind some data as I have discussed in my earlier article i.e. How to bind combobox with database. Combo Box is used to display a single field at a time and a list can contains multiple fields. I will use the same student class as in my earlier articles.

To show a single field, combo box have a property DisplayMember that will specify the field to display. In the following C# code the student list will bind with the combo box and name field will be shown.

List<Student> stuList = new List<Student>();
cmbBox.DataSource = stuList;
cmbBox.DisplayMember = "Name";

Before running this code, you have to sure that your list have some records to show. It will not throw an exception, but it will also not show any records because the list is empty.

Run the project and combo box will bind the list items and we can select any of them. The image shown the combo box items:

How to bind combobox with list of items: C# Windows forms

Rabu, 30 Oktober 2013

GridView Paging in ASP.NET

Introduction

There are many things to remember that how a GridView support paging at runtime. Also DOTNET provides many styles is to related paging such as numbering , first and last order etc. Always first think in mind that SqlDataReader does not support paging so do not bind your GridView control with DataReader.

Here we are describe easy steps to bind GridView with paging.

Step-1: Drop GridView Control on Designing window and add some attributes in GridView Tag.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false"
            AllowPaging ="true" PageSize ="2"
            onpageindexchanging="GridView1_PageIndexChanging1">

Step-3:  Handle onpageindexchanging event in code file.

protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        loadgrid();

    }
Step-4 : Run your application

Complete code

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

<!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:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false"
            AllowPaging ="true" PageSize ="2"
            onpageindexchanging="GridView1_PageIndexChanging1">
        <Columns>
        <asp:TemplateField HeaderText ="Action">
        <ItemTemplate >
            <asp:CheckBox ID="Chkdelete" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>

           <asp:TemplateField HeaderText ="Id" Visible =false>
        <ItemTemplate >
            <asp:Label ID="snolbl" runat="server" Text='<%# Eval("sno") %>' />
        </ItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText ="Name">
        <ItemTemplate >
            <asp:Label ID="namelbl" runat="server" Text='<%# Eval("Name") %>' />
        </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText ="Address">
        <ItemTemplate >
      <asp:Label ID="addlbl" runat="server" Text='<%# Eval("Name") %>' />
        </ItemTemplate>
        </asp:TemplateField>
        
        </Columns>
        </asp:GridView>
   
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Delete Multiple rows" />
   
    </div>
    </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.Configuration;
using System.Data;

public partial class deleteItem : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadgrid();
        }
    }

    private void loadgrid()
    {
        SqlCommand cmd = new SqlCommand("Select * from deltable",con);
        con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        GridView1.DataSource = ds;

        GridView1.DataBind();
        con.Close();

       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow  row in GridView1 .Rows)
        {
            var check = row.FindControl("Chkdelete") as CheckBox;
            if (check .Checked)
            {
                var id = row.FindControl("snolbl") as Label;
                SqlCommand cmd = new SqlCommand("delete from deltable where sno=@s", con);
                cmd.Parameters.AddWithValue("@s", id.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

               
            }
           
        }
        loadgrid();
    }

    protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        loadgrid();

    }

}


Output
GridView Paging in ASP.NET
GridView Paging in ASP.NET


ASP.NET Exception : The data source does not support server-side paging

The data source does not support server-side paging
SqlDataReader class does not support paging in ASP.NET.  In above snapshot you can see that your GridView is bind with DataReader instance so if you want to remove this error , replace your DataReader with DataSet or DataTable.

Here are given some easy steps to bind your GridView with Dataset.

 Use DataSet for enable paging

DataSet ds = new DataSet();
        SqlDataAdapter da = newSqlDataAdapter(cmd);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

Paging Example in ASP.NET

Selasa, 29 Oktober 2013

Difference Between SGML, HTML, and XML

XML , markup languages such as standard Generalized Markup Language(SGML) and Hypertext Markup Language(HTML) are also available.

SGML was released in 1980. It allows documents to describe their grammar by specifying the tag set used in the document and the structural relationship that these tags represent. This makes it possible to define individual formats for documents, handle large and complex documents, and manage large information repositories. However, SGML is complex and difficult for developers to master.

HTML was created by Tim Berners-Lee in 1989 as a simple and effective way of generating clear and readable documents. HTML enables you to create documents and Web pages that can be read by all web browsers . It uses a set of tags in conformance with the SGML specification.

The World Wide web consortium (W3C) developed XML to enable the expansion of Web technologies into the new domains of document processing and data interchange. XML is a subset of SGML. It is designed to ease data exchange over the Internet.  Though HTML and XML are markup languages, they have different purposes. HTML is used for data presentation, whereas XML is used for data description and definition.


Getting Started with XML

Introduction

Traditionally, organizations have conducted business on paper. Preprinted formats were most widely used to exchange information between businesses. As the number of transactions between different organizations increased over the years, there was a need for a more effective way of communicating and processing business data. Electronic Data Interchange (IDE) emerged as a result of this need. EDI refers to the process of exchanging documents in a standard format between two computer systems.

The two widely used EDI standards for transmitting data between computers are ANSI X12 and UN/EDIFACT. if data is transmitted using EDI standards, the data can be translated to or from the end-user's application format with the help of EDI software, such as RealWorld and Macola.

Consider an example of two companies, X and Y, using RealWorld and Macola, respectively to convert their files into EDI formats. Without an EDI standard, company Y cannot translate the EDI files received from company X into Macola-compatible format. This is because the EDI formats of RealWorld and Macola are dissimilar, However, if the files formatted using RealWorld are converted into one of the EDI standards, then company Y can translate invoice files received from company X into Macola-compatible format.

Although EDI standards provide an effective solution for e-commerce transactions, they Have not been widely accepted due to the following  limitations:

  • Rigid transaction set: Traditional EDI was built on fixed transaction sets. Consider The following example. Company A’s invoice bill currently includes the customer Name, company name, phone number, and cash amount. If one of its trading partners Starts accepting  credit cards, then company A has to modify the invoice bill to Include credit card details. If Company A uses EDI to exchange data, then the EDI Format has to reflect this change. This is a tedious and time-consuming process, Because the Accredited Standards Committee X12  sub-group of ANSI or the UN/EDIF ACT working group must recognize the new format. Therefore, the fixed Transaction set becomes a bottleneck to business units that evolve new services, Products, and business processes.

  • Fixed business rules: The business rules of small, medium, and large business units Of the same industry widely vary. Due to this, the same set of  EDI standards cannot Be uniformly implemented across all of them.
  • High costs: It can be expensive for small- and medium-size business units to Implement EDI standards as compared to large business units, because of high Networking costs. Even large business units that have high investments in automation Will not be ready to replace their systems based on EDI standards. Therefore, Acceptance of EDI is restricted to a few business units who are willing to invest in EDI.
  • Slow pace of standards evolution : As EDI standards cater to companies with different needs, the process of developing these standards is time consuming. In addition, the standards may not cater to the needs of all companies.
Therefore, EDI does not server as a cost-effective solution to implement data interchange among heterogeneous systems.

Introducing XML

XML is a text-based markup language that enables you to store data in a structured format by using meaningful tags. The term "extensible" implies that you can extend your ability to describe a document by defining meaningful tags for your application. XML is a cross-platform, hardware and software independent markup language. It enables computers to transfer structured data between heterogeneous systems. XML is used as a common data interchange format in a number of applications. In the example of the B2B e-commerce model, xml can be used to exchange data between the trading partners, thus eliminating the problems faced by EDI.

Web Architecture Using XML

In a traditional Web architecture, a client sends a request to the server in a predefined format and receives the appropriate response. The advantage of using XML in Web architecture is that the structure of the request can be obtained from the server at runtime. This is possible because the data stored in a XML document does not assume its intended use. Different applications can extract data according to their customized needs. Since XML is used to exchange data between various Web applications, the coupling between the server application and the client application is relatively loose.

XML can encode non-relational data, as well as, relational data structures. This enables the server application to extract data from any data source, and helps the programmers to quickly build applications for the manipulation of that data.

XML Web Architecture
XML Web Architecture

Senin, 28 Oktober 2013

ASP.NET : Bind CheckBoxList with Multiple DataSource

Introduction 

The term DataSource means you can store multiple data at one place known as DataSource. There are different DataSource available in DOTNET library such as DataTable , SqlDataReader, Array , Collections etc. Now at time, lets take a simple example to bind single CheckBoxList with multiple DataSource.
 Algorithm behind the scene
Step-1 : Drop CheckBoxList and Two button control onto the design page.
Step-2 : On first button click event we can bind CheckBoxList with one string array
Step-3 : On Second button click event we can bind same CheckBoxList with another string array.

Complete Code

<%@ PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2"%>

<!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>Example of Multiple DataSource</title>
    <style type="text/css">
        .style1 {
            font-size: larger;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <strong><span class="style1">Example of Multiple DataSource</span></strong><br />
        <asp:CheckBoxList ID="CheckBoxList1"runat="server">
        </asp:CheckBoxList>
        <br />
        <asp:Button ID="Button1"runat="server"onclick="technology"
            Text="Technology"Width="87px"/>
    &nbsp;<asp:Button ID="Button2" runat="server" onclick="gadgets"
            Text="Gadgets"Width="73px"/>
    </div>
    </form>
</body>
</html>

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

public partial class Default2 : System.Web.UI.Page
{
    protected voidPage_Load(object sender, EventArgs e)
    {

    }
  
    protected voidtechnology(object sender, EventArgs e)
    {
        string[] tech = { "Turbo c", "Java", "dotnet" };
        CheckBoxList1.DataSource = tech;
        CheckBoxList1.DataBind();

    }
    protected voidgadgets(object sender, EventArgse)
    {
        string[] gadgets = { "Phone", "Camera", "Tablet"};
        CheckBoxList1.DataSource = gadgets;
        CheckBoxList1.DataBind();
    }

}

Output

ASP.NET : Bind CheckBoxList with Multiple DataSource

ASP.NET : Bind CheckBoxList with Multiple DataSource

Programmatically change CheckBoxList enable disable property in ASP.NET

In previous example we have been covered public properties of CheckBoxList control such as
<%@ PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2"%>

<!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>Example of Enable disable property of CheckBoxList</title>
    <style type="text/css">
        .style1 {
            font-size: larger;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <strong><span class="style1">Example of Enable disable property of CheckBoxList</span></strong><br />
        <asp:CheckBoxList ID="CheckBoxList1"runat="server"Width="168px">
            <asp:ListItem>AdRotator Control</asp:ListItem>
            <asp:ListItem>BulletedList Control</asp:ListItem>
            <asp:ListItem>Button Control</asp:ListItem>
            <asp:ListItem>Calendar Control</asp:ListItem>
        </asp:CheckBoxList>
        <br />
        <asp:Button ID="Button1"runat="server"onclick="Enable"
            Text="Enable"Width="61px"/>
    &nbsp;<asp:Button ID="Button2" runat="server" onclick="Disable"
            Text="Disable"Width="61px"/>
    </div>
    </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;

public partial class Default2 : System.Web.UI.Page
{
    protected voidPage_Load(object sender, EventArgs e)
    {

    }
  
    protected voidDisable(object sender, EventArgse)
    {
        CheckBoxList1.Enabled = false;
    }
    protected voidEnable(object sender, EventArgse)
    {
        CheckBoxList1.Enabled = true;
    }
}

Output

Programmatically change CheckBoxList enable disable property in ASP.NET

Programmatically change CheckBoxList enable disable property in ASP.NET

Programmatically change CheckBoxList font name in ASP.NET

CheckBoxList control is the collection of CheckBoxes . Now you can perform some operations on each CheckBox using Index. Application of CheckBoxList control.

  • You can select multiple items.
Programmatically change CheckBoxList font name in ASP.NET

Complete code

<%@ PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2"%>

<!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>Change font name of the CheckBox List</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBoxList ID="CheckBoxList1"runat="server"Width="168px">
            <asp:ListItem>AdRotator Control</asp:ListItem>
            <asp:ListItem>BulletedList Control</asp:ListItem>
            <asp:ListItem>Button Control</asp:ListItem>
            <asp:ListItem>Calendar Control</asp:ListItem>
        </asp:CheckBoxList>
        <br />
        <asp:Button ID="Button1"runat="server"onclick="Button1_Click"
            Text="Change Font name" Width="124px"/>
    </div>
    </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;

public partial class Default2 : System.Web.UI.Page
{
    protected voidPage_Load(object sender, EventArgs e)
    {

    }
    protected voidButton1_Click(object sender, EventArgs e)
    {
        CheckBoxList1.Font.Name = "Algerian";

    }
}
Output
Programmatically change CheckBoxList font name in ASP.NET

Programmatically change CheckBoxList font name in ASP.NET