Saturday, April 5, 2014

How to insert multiple rows into the GridView without using any database in ASP.NET using VB.NET/C# || how to save multiple rows in GridView into a Session.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   Username: <asp:TextBox ID="TextBox1" runat="server"/><br />
   Sitename: <asp:TextBox ID="TextBox2" runat="server"/><br />
   Facebook Id: <asp:TextBox ID="TextBox3" runat="server"/><br />
   Twitter Id: <asp:TextBox ID="TextBox4" runat="server"/><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="RGrid" runat="server" >
</asp:GridView>
    </div>
    </form>
</body>
</html>


Code for VB.NET:



Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Partial Public Class multiGrid
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub
    Private Sub BindGrid(ByVal rowcount As Integer)
        Dim dt As New DataTable()
        Dim dr As DataRow
  dt.Columns.Add(New System.Data.DataColumn("UserName", GetType([String])))
  dt.Columns.Add(New System.Data.DataColumn("Sitename", GetType([String])))
dt.Columns.Add(New System.Data.DataColumn("Facebook id", GetType([String])))
   dt.Columns.Add(New System.Data.DataColumn("Twitter id", GetType([String])))
        If Session("CurrentData") IsNot Nothing Then
            For i As Integer = 0 To rowcount
                dt = DirectCast(Session("CurrentData"), DataTable)
                If dt.Rows.Count > 0 Then
                    dr = dt.NewRow()
                    dr(0) = dt.Rows(0)(0).ToString()
                    dr(1) = dt.Rows(0)(1).ToString()
                    dr(2) = dt.Rows(0)(2).ToString()
                    dr(3) = dt.Rows(0)(3).ToString()
                End If
            Next
            dr = dt.NewRow()
            dr(0) = TextBox1.Text
            dr(1) = TextBox2.Text
            dr(2) = TextBox3.Text
            dr(3) = TextBox4.Text
            dt.Rows.Add(dr)
        Else
            dr = dt.NewRow()
            dr(0) = TextBox1.Text
            dr(1) = TextBox2.Text
            dr(2) = TextBox3.Text
            dr(3) = TextBox4.Text
            dt.Rows.Add(dr)
        End If
        ' If Session has a data then use the value as the DataSource
        If Session("CurrentData") IsNot Nothing Then
            RGrid.DataSource = DirectCast(Session("CurrentData"), DataTable)
            RGrid.DataBind()
        Else
            ' Bind GridView with the initial data assocaited in the DataTable
            RGrid.DataSource = dt
            RGrid.DataBind()
        End If
        ' Store the DataTable in Session to retain the values
        Session("CurrentData") = dt
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Check if the Session has a data assoiciated within it. If
        If Session("CurrentData") IsNot Nothing Then
            Dim dt As DataTable = DirectCast(Session("CurrentData"), DataTable)
            Dim count As Integer = dt.Rows.Count
            BindGrid(count)
        Else
            BindGrid(1)
        End If
    End Sub
End Class

Code for C#.NET:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class multiGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    private void BindGrid(int rowcount)
    {
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("UserName", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Sitename", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Facebook id", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Twitter id", typeof(String)));
        if (Session["CurrentData"] != null)
        {
            for (int i = 0; i < rowcount + 1; i++)
            {
                dt = (DataTable)Session["CurrentData"];
                if (dt.Rows.Count > 0)
                {
                    dr = dt.NewRow();
                    dr[0] = dt.Rows[0][0].ToString();
                    dr[1] = dt.Rows[0][1].ToString();
                    dr[2] = dt.Rows[0][2].ToString();
                    dr[3] = dt.Rows[0][3].ToString();
                }
            }
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
            dr[2] = TextBox3.Text;
            dr[3] = TextBox4.Text;
            dt.Rows.Add(dr);
        }
        else
        {
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
            dr[2] = TextBox3.Text;
            dr[3] = TextBox4.Text;
            dt.Rows.Add(dr);
        }
        // If Session has a data then use the value as the DataSource
        if (Session["CurrentData"] != null)
        {
            RGrid.DataSource = (DataTable)Session["CurrentData"];
            RGrid.DataBind();
        }
        else
        {
            // Bind GridView with the initial data assocaited in the DataTable
            RGrid.DataSource = dt;
            RGrid.DataBind();
        }
        // Store the DataTable in Session to retain the values
        Session["CurrentData"] = dt;
    }
      protected void Button1_Click(object sender, EventArgs e)
    {
        // Check if the Session has a data assoiciated within it. If
        if (Session["CurrentData"] != null)
        {
            DataTable dt = (DataTable)Session["CurrentData"];
            int count = dt.Rows.Count;
            BindGrid(count);
        }
        else
        {
            BindGrid(1);
        }
    
    }
}


Output:



Gridview1

0 comments:

Post a Comment