Friday 17 April 2015

How to get / fetch data from GridView and store in DataTable Dynamically in ASP.NET using C#.NET?

How to get / fetch data from GridView and store in DataTable Dynamically in ASP.NET using C#.NET?

In this article I will explain how to fetch / get data from a GridView and how to store it into a DataTable dynamically using C#.NET in ASP.NET

Fallow the below steps:

First, we have to create GridView with required Columns and set its AutoGenerateColumns property to False. If you required footer then show footer is true otherwise false and same as paging also.

Design the GridViewas per your reqirements. Use Templates like Item Template, Edit Item Template, and Footer Templates to Create GridView.

How to fetch data from GridView and store in DataTable?

How to fetch data from GridView and store in DataTable in ASP.NET?

In this article I will explain how to fetch data from a GridView and how to store it into a DataTable in ASP.NET

Fallow the below steps:

First, we have to create GridView and set its AutoGenerateColumns property to False.

Design the GridViewas per your reqirements. Use Templates like Item Template, Edit Item 
Template, and Footer Templates to Create GridView.


How to bind DataTable data to Gridview using C#.NET with ASP.NET Dynamically.

How to bind DataTable data to Gridview using C#.NET with ASP.NET Dynamically.

In this article I will explain how to create DataTable and how to bind DataTable data to Gridview Using C#.NET.

To bind DataTable data to gridview we need to write the code like as shown below like ….

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

Add below namespaces in code behind…

using System.Data;

 protected void Page_Load(object sender, EventArgs e)
{
BindDataTabelToGridviewData();
}

/// <summary>
/// Dynamically create DataTable & bind data to gridview
/// </summary>

protected void BindGridviewData()
{
        DataTable dtDynamic = new DataTable();

       DataColumn dc = new DataColumn();

        dc = new DataColumn();
        dc.ColumnName = "MatTransSlNo";
        dc.DataType = Type.GetType("System.String");
        dtDynamic.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "Amount";
        dc.DataType = Type.GetType("System.Decimal");
        dtDynamic.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "DeleteFlag";
        dc.DataType = Type.GetType("System.Int32");
        dtDynamic.Columns.Add(dc);

dtDynamic.Rows.Add(“1”, 101.10,1);
dtDynamic.Rows.Add(“2”, 102.10,0);
dtDynamic.Rows.Add(“3”, 103.10,1);
dtDynamic.Rows.Add(“4”, 104.50,1);

GridView1.DataSource = dtDynamic;
GridView1.DataBind();
}

How to create DataTable with Data types in ASP.NET?

How to create DataTable with Data types in ASP.NET?

In this article I will explain how to create DataTable with Data types in ASP.NET and C#.NET

First we need to add below namespace for creating DataTable in C#.NET.

using System.Data;

Declare a DataTable as below

DataTable dtDynamic = new DataTable();

Below code is useful for creating DataTable with Parameters.

void CreateDynamicTable()
    {

        DataColumn dc = new DataColumn();

        dc = new DataColumn();
        dc.ColumnName = "MatTransSlNo";
        dc.DataType = Type.GetType("System.String");
        dtDynamic.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "Amount";
        dc.DataType = Type.GetType("System.Decimal");
        dtDynamic.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "DeleteFlag";
        dc.DataType = Type.GetType("System.Int32");
        dtDynamic.Columns.Add(dc);


    }

Tuesday 14 April 2015

How to increase session timeout in ASP.NET?


In this article I will explain how to increase the session timeout in ASP.NET.

You can increase the session time-out in ASP.NET in mainly three following ways in IIS 7 Version.

Method – I:

First open up IIS

Select your website.

Click on Session state on the right

Now enter your session timeout under the cookie settings

Method – II:

Open up your web.config file and under the system.web section add the following:

<sessionState timeout = "20" mode = "InProc" />

Replace 20 with whatever number you wish.

Ex: <sessionState timeout = "60" mode = "InProc" /> // 1 Hour

Method – III:

            Open Global.asax file in your web site.

Under the Session_Start method, set the timeout property of the session to the required value like this.


Session.Timeout = "60";


How to get client MAC address in ASP.NET?


 In this article I will explain how to get the client MAC address in ASP.NET using C#.NET.

We may have a requirement to get Client MAC address. We implemented the blow code for Client MAC address.

Add below namespaces to your aspx.cs page.

using System.Net;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;


public partial class _Default : Page
{

    [DllImport("Iphlpapi.dll")]
    private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
    [DllImport("Ws2_32.dll")]
    private static extern Int32 inet_addr(string ip);

In the Page_Load event method add in the following code:

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string userip = Request.UserHostAddress;
            string strClientIP = Request.UserHostAddress.ToString().Trim();
            Int32 ldest = inet_addr(strClientIP);
            Int32 lhost = inet_addr("");
            Int64 macinfo = new Int64();
            Int32 len = 6;
            int res = SendARP(ldest, 0, ref macinfo, ref len);
            string mac_src = macinfo.ToString("X");
          
            while (mac_src.Length < 12)
            {
                mac_src = mac_src.Insert(0, "0");
            }

            string mac_dest = "";

            for (int i = 0; i < 11; i++)
            {
                if (0 == (i % 2))
                {
                    if (i == 10)
                    {
                        mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
                    }
                    else
                    {
                        mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
                    }
                }
            }

            Response.Write(MAC address is" + mac_dest + "."

             + "<br>");
        }
        catch (Exception err)
        {
            Response.Write(err.Message);
        }
    }

}


How to implement a Multiple Select/Choose (MultiSelect) DropDownList with CheckBoxes in ASP.Net.

In this article I will explain how to implement a Multiple Select/Choose (MultiSelect) DropDownList with CheckBoxes in ASP.Net.

(Or)

Checkbox list in Dropdown using Asp.net Ajax PopupControlExtender control





We may have a requirement to select more than one value from dropdown list but we can’t select/choose the more than one value from asp.net dropdown list control.

It’s an asp.net Ajax control which is used to attach any control to display with popup window and we can add server or html controls to display in popup window. This Popupcontrolextender control needs to be placed inside the update panel as shown below in sample code. In this control there are three important properties we need to use to achieve this functionality such as TargetControlID, PopupControlID and Position

Below is the style CSS code for Ajax Popupcontrolextender .


What Regular expression validator is and how to validate the Phone or Mobile numbers ASP.NET

In this article I will explain what Regular expression validator is and how to validate the Phone or Mobile numbers ASP.NET

Regular expression validator is the most common validation expressions used to validate an Email ID and phone number.

It is necessary to specify that RegularExpressionValidator is validating which control. In theControlToValidate property, we specify the control which will be validated. For this, we need to mention the ID of the textbox control.

India Mobile validation expression: ValidationExpression="^[\s\S]{10,12}$"
US Mobile validation expression: ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"

Example:

 
  <asp:TextBox ID="txtMobile" runat="server" CssClass="txtbox" Width="85%" MaxLength="12"></asp:TextBox>
                                    <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="txtMobile" ID="RegularExpressionValidator3"
                                        ValidationExpression="^[\s\S]{10,12}$" runat="server" ValidationGroup="vgSave" ForeColor="Red"
                                        ErrorMessage="Minimum 10 and Maximum 12 characters required.">*</asp:RegularExpressionValidator>
                                    <ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtender4" runat="server" TargetControlID="txtMobile"
                                        FilterType="Custom, Numbers" Enabled="True" />
                                    <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator6" ControlToValidate="txtMobile"
                                        Display="Dynamic" ErrorMessage="Mobile is required" ForeColor="Red" ValidationGroup="vgSave">*

                                    </asp:RequiredFieldValidator>