Contact Form

Name

Email *

Message *

GridView Datatable using Session

No comments

 <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Index.aspx.vb" Inherits="WebApplication11.Index" %>  
 <!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>  
   <style type="text/css">  
     .style1  
     {  
       width: 100%;  
     }  
   </style>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <table class="style1">  
       <tr>  
         <td colspan="4" align="center">  
           Student Record</td>  
       </tr>  
       <tr>  
         <td>  
           &nbsp;</td>  
         <td>  
           &nbsp;</td>  
         <td>  
           <asp:Button ID="btnsave" runat="server" Text="Save" />  
         </td>  
         <td>  
           &nbsp;</td>  
       </tr>  
       <tr>  
         <td>  
           &nbsp;</td>  
         <td align="right">  
           Student Name :</td>  
         <td>  
           <asp:TextBox ID="txtSname" runat="server"></asp:TextBox>  
         </td>  
         <td>  
           &nbsp;</td>  
       </tr>  
       <tr>  
         <td>  
           &nbsp;</td>  
         <td align="right">  
           Student Address:</td>  
         <td>  
           <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>  
         </td>  
         <td>  
           &nbsp;</td>  
       </tr>  
     </table>  
   </div>  
   <table class="style1">  
     <tr>  
       <td>  
         &nbsp;</td>  
       <td>  
         &nbsp;</td>  
     </tr>  
     <tr>  
       <td align="right">  
         Subject Name :</td>  
       <td>  
           <asp:TextBox ID="txtSubjectname" runat="server"></asp:TextBox>  
         </td>  
     </tr>  
     <tr>  
       <td align="right">  
         Teacher Name :</td>  
       <td>  
           <asp:TextBox ID="txtTeacher" runat="server"></asp:TextBox>  
         </td>  
     </tr>  
     <tr>  
       <td>  
         &nbsp;</td>  
       <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>  
       <td>  
       <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
           <asp:Button ID="btnadd" runat="server" Text="Add" />  
         </td>  
     </tr>  
   </table>  
   <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="false" OnRowDeleting="Gridview1_RowDeleting" >  
   <Columns>  
    <asp:BoundField DataField="Subject" HeaderText="Subject" ItemStyle-Width="150" />  
     <asp:BoundField DataField="Teacher" HeaderText="Teacher" ItemStyle-Width="150" />  
     <asp:TemplateField>  
             <ItemTemplate>  
               <asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete" >Delete</asp:LinkButton>  
             </ItemTemplate>  
           </asp:TemplateField>  
            <asp:TemplateField>  
             <ItemTemplate>  
               <asp:LinkButton ID="lnkEdit" runat="server" CommandName="Select" >Edit</asp:LinkButton>  
             </ItemTemplate>  
           </asp:TemplateField>  
    <%--  <asp:TemplateField>  
       <ItemTemplate>  
         <asp:LinkButton ID="LinkButton4" Text="Delete" runat="server" OnClick="OnDelete" />  
       </ItemTemplate>  
       </asp:TemplateField>--%>  
       </Columns>  
   </asp:GridView>  
   </form>  
 </body>  
 </html>  
 Imports System.Data.SqlClient  
 Public Class Index  
   Inherits System.Web.UI.Page  
   Public strConnectionString As String = ConfigurationManager.ConnectionStrings("DBstudent").ConnectionString  
   Public objConnection As New SqlConnection(strConnectionString)  
   Dim qry As String = ""  
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
     If Not IsPostBack Then  
       Dim myDataTable As New DataTable()  
       myDataTable = CreateDataTable()  
       Session("myDatatable") = myDataTable  
       Label2.Text = "-1"  
     End If  
   End Sub  
   Protected Sub btnadd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnadd.Click  
     AddDataToTable(Label2.Text, txtSubjectname.Text.Trim(), txtTeacher.Text.Trim(), DirectCast(Session("myDatatable"), DataTable))  
     Me.GridView1.DataSource = DirectCast(Session("myDatatable"), DataTable).DefaultView  
     Me.GridView1.DataBind()  
   End Sub  
   Private Sub AddDataToTable(ByVal indexid As String, ByVal firstname As String, ByVal lastname As String, ByVal myTable As DataTable)  
     If Convert.ToDouble(indexid) > 0 Then  
       myTable.Rows(indexid)("Subject") = firstname  
       myTable.Rows(indexid)("Teacher") = lastname  
       Label2.Text = "-1"  
     Else  
       Dim row As DataRow  
       row = myTable.NewRow()  
       row("Sdtid") = 0  
       row("Stid") = 0  
       row("Subject") = firstname  
       row("Teacher") = lastname  
       myTable.Rows.Add(row)  
     End If  
   End Sub  
   Private Function CreateDataTable() As DataTable  
     Dim dt As New DataTable()  
     dt.Columns.AddRange(New DataColumn() {New DataColumn("Sdtid", GetType(Integer)),  
                        New DataColumn("Stid", GetType(Integer)),  
                         New DataColumn("Subject", GetType(String)),  
                         New DataColumn("Teacher", GetType(String))})  
     Return dt  
   End Function  
   Protected Sub Gridview1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)  
     Dim dt As DataTable = DirectCast(Session("myDatatable"), DataTable)  
     dt.Rows.Remove(dt.Rows(e.RowIndex))  
     Session("myDatatable") = dt  
     Me.GridView1.DataSource = DirectCast(Session("myDatatable"), DataTable).DefaultView  
     Me.GridView1.DataBind()  
   End Sub  
   'Protected Sub OnDelete(ByVal sender As Object, ByVal e As EventArgs)  
   '  Dim row As GridViewRow = GridView1.Rows(Index)  
   '  'Dim row As GridViewRow = TryCast(TryCast(sender, LinkButton).NamingContainer, GridViewRow)  
   '  Dim dt As DataTable = DirectCast(Session("myDatatable"), DataTable)  
   '  dt.Rows.Remove(dt.Rows(row.RowIndex))  
   '  Session("myDatatable") = dt  
   '  Me.GridView1.DataSource = DirectCast(Session("myDatatable"), DataTable).DefaultView  
   '  Me.GridView1.DataBind()  
   'End Sub  
   'Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing  
   '  GridView1.EditIndex = e.NewEditIndex  
   '  Label1.Text = e.NewEditIndex  
   '  Dim dt As DataTable = DirectCast(Session("myDatatable"), DataTable)  
   '  Dim dr As DataRow = dt.Rows(Label1.Text)  
   '  txtSubjectname.Text = dr("Subject")  
   '  txtTeacher.Text = dr("Teacher")  
   'End Sub  
   Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnsave.Click  
     qry = " Insert into StudentMain (Sname,SAddress) Values ('" & txtSname.Text & "' ,'" & txtAddress.Text & "') ;select @@IDentity as myID;"  
     Dim i As Integer = ExecuteScalar(qry)  
     Dim dt As DataTable = DirectCast(Session("myDatatable"), DataTable)  
     For Each dr As DataRow In dt.Rows  
       qry = " insert into StudentDetail (Stid,Subject,Teacher) Values (" & i & ",'" & dr("Subject") & "' ,'" & dr("Teacher") & "')"  
       ExecuteNonQuery(qry)  
     Next  
   End Sub  
   Public Function ExecuteScalar(ByVal SqlString As String) As String  
     Dim str As String = ""  
     Dim dbcmd As New SqlCommand(SqlString, objConnection)  
     Try  
       dbcmd.Connection.Open()  
       str = dbcmd.ExecuteScalar()  
     Catch ex As SqlClient.SqlException  
       Throw ex  
     Finally  
       dbcmd.Connection.Close()  
     End Try  
     Return str  
   End Function  
   Public Sub ExecuteNonQuery(ByVal SqlString As String)  
     Dim dbcmd As New SqlCommand(SqlString, objConnection)  
     Try  
       dbcmd.Connection.Open()  
       dbcmd.CommandTimeout = 0  
       dbcmd.ExecuteNonQuery()  
     Catch ex As SqlClient.SqlException  
       Throw ex  
     Finally  
       dbcmd.Connection.Close()  
     End Try  
   End Sub  
   'Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)  
   '  Dim row As GridViewRow = GridView1.Rows(e.RowIndex)  
   '  Label2.Text = row.RowIndex  
   '  '  'Dim row As GridViewRow = TryCast(TryCast(sender, LinkButton).NamingContainer, GridViewRow)  
   'End Sub  
   'Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing  
   '  Dim row As GridViewRow = GridView1.Rows(e.NewEditIndex)  
   '  Label2.Text = row.RowIndex  
   'End Sub  
   'Protected Sub GridView1_RowSelecting(ByVal sender As Object, ByVal e As EventArgs)  
   '  Dim row As GridViewRow = GridView1.Rows(e.ne)  
   'End Sub  
   Protected Sub GridView1_RowSelecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging  
     Dim row As GridViewRow = GridView1.Rows(e.NewSelectedIndex)  
     Label2.Text = row.RowIndex  
     Dim dt As DataTable = DirectCast(Session("myDatatable"), DataTable)  
     Dim dr As DataRow = dt.Rows(Label2.Text)  
     txtSubjectname.Text = dr("Subject")  
     txtTeacher.Text = dr("Teacher")  
   End Sub  
 End Class  

No comments :

Post a Comment