Hi sofia,
Check this example. Now please take its reference and correct your code.
HTML
Home
<div class="container" style="padding: 20px; height: auto; background-color: darkseagreen; border-radius: 10px 6px">
<div>
<div class="centerHaeder" style="text-align: center">
<h1>BOOK STORE</h1>
</div>
<hr />
<div style="text-align: center">
<p><i>What's your favourite book!</i></p>
</div>
</div>
<div class="col-sm-12">
<b>
<asp:Label ID="Label1" runat="server" Text="Book Name"></asp:Label></b><br />
<asp:TextBox ID="txtBookName" runat="server" Width="100%" CssClass="form-control"></asp:TextBox>
</div>
<div class="col-sm-12">
<br />
<b>
<asp:Label ID="Label2" runat="server" Text="Author Name"></asp:Label></b><br />
<asp:TextBox ID="txtAuthorName" runat="server" Width="100%" CssClass="form-control"></asp:TextBox>
</div>
<div class="col-sm-12" style="text-align: center">
<br />
<asp:Button ID="btnAddBook" runat="server" Text="Add Book" OnClick="btnAddBook_Click" CssClass="btn-success" />
</div>
</div>
BookList
<div class="container">
<h2>List Of Books And Authors</h2>
<div>
BookName
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
AuthorName
<asp:Label ID="Label2" runat="server" Text=""></asp:Label><br />
<a href="Home.aspx" runat="server">Go Back</a>
</div>
<div class="col-sm-12">
<asp:GridView ID="grdBookList" runat="server" CssClass="table table-striped table-bordered table-hover"
AllowPaging="True" AutoGenerateSelectButton="True" Width="100%" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#005D28" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#005D28" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</div>
</div>
Code
Home
C#
protected void btnAddBook_Click(object sender, EventArgs e)
{
string bookname = txtBookName.Text;
string authorname = txtAuthorName.Text;
Session["BookName"] = bookname;
Session["AuthorName"] = authorname;
Response.Redirect("~/BookList.aspx");
}
VB.Net
Protected Sub btnAddBook_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim bookname As String = txtBookName.Text
Dim authorname As String = txtAuthorName.Text
Session("BookName") = bookname
Session("AuthorName") = authorname
Response.Redirect("~/BookList.aspx")
End Sub
BookList
C#
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["BookName"].ToString();
Label2.Text = Session["AuthorName"].ToString();
BindGridView(Session["BookName"].ToString(), Session["AuthorName"].ToString());
}
private void BindGridView(string bookname, string authorname)
{
System.Data.DataRow Dr = GridData.NewRow();
Dr["Book Name"] = bookname;
Dr["Author Name"] = authorname;
GridData.Rows.Add(Dr);
grdBookList.DataSource = GridData;
grdBookList.DataBind();
}
public System.Data.DataTable GridData
{
get
{
if (Session["GridData"] == null)
{
System.Data.DataTable dt = new System.Data.DataTable("GridData");
dt.Columns.Add(new System.Data.DataColumn("Book Name"));
dt.Columns.Add(new System.Data.DataColumn("Author Name"));
Session["GridData"] = dt;
return dt;
}
else
{
return (System.Data.DataTable)Session["GridData"];
}
}
set { Session["GridData"] = value; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Label1.Text = Session("BookName").ToString()
Label2.Text = Session("AuthorName").ToString()
BindGridView(Session("BookName").ToString(), Session("AuthorName").ToString())
End Sub
Private Sub BindGridView(ByVal bookname As String, ByVal authorname As String)
Dim Dr As System.Data.DataRow = GridData.NewRow()
Dr("Book Name") = bookname
Dr("Author Name") = authorname
GridData.Rows.Add(Dr)
grdBookList.DataSource = GridData
grdBookList.DataBind()
End Sub
Public Property GridData As System.Data.DataTable
Get
If Session("GridData") Is Nothing Then
Dim dt As System.Data.DataTable = New System.Data.DataTable("GridData")
dt.Columns.Add(New System.Data.DataColumn("Book Name"))
dt.Columns.Add(New System.Data.DataColumn("Author Name"))
Session("GridData") = dt
Return dt
Else
Return CType(Session("GridData"), System.Data.DataTable)
End If
End Get
Set(ByVal value As System.Data.DataTable)
Session("GridData") = value
End Set
End Property
Screenshot