Hi rajeesh,
Instead of ViewState you have to use Session. When you redirect to Products Page for continue shopping ViewState will be lost. So you have to use Session state to keep the data accross all the page untill you set it to null.
I have just created a small example for your understand. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
ProductsPage
<asp:GridView runat="server" ID="gvProducts" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:HyperLinkField DataNavigateUrlFormatString="~/ViewDetails.aspx?Id={0}" DataNavigateUrlFields="ProductID"
Text="View Details" HeaderText="Details" />
</Columns>
</asp:GridView>
DetailsPage
<asp:GridView runat="server" ID="gvDetails" />
<br />
<asp:Button ID="btnAdd" Text="Add to Cart" runat="server" OnClick="Add" />
<asp:Button ID="btnContinue" Text="Continue Shopping" runat="server" OnClick="Continue" />
<br />
<asp:GridView runat="server" ID="gvCartItems" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
ProductsPage
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT TOP 10 * FROM Products";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
gvProducts.DataSource = dt;
gvProducts.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT TOP 10 * FROM Products"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
gvProducts.DataSource = dt
gvProducts.DataBind()
End Using
End Using
End If
End Sub
ViewDetailsPage
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id = Convert.ToInt32(Request.QueryString["Id"]);
DataTable dt = GetData(id);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
protected void Add(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["Id"]);
DataTable dt = GetData(id);
if (Session["Data"] != null)
{
DataTable dx = (DataTable)Session["Data"];
dx.Merge(dt);
Session["Data"] = dx;
gvCartItems.DataSource = dx;
gvCartItems.DataBind();
}
else
{
Session["Data"] = dt;
gvCartItems.DataSource = dt;
gvCartItems.DataBind();
}
}
protected void Continue(object sender, EventArgs e)
{
Response.Redirect("Products.aspx");
}
private DataTable GetData(int id)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT ProductId,ProductName,UnitPrice FROM Products WHERE ProductID = @Id";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", id);
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Dim id As Integer = Convert.ToInt32(Request.QueryString("Id"))
Dim dt As DataTable = GetData(id)
gvDetails.DataSource = dt
gvDetails.DataBind()
End If
End Sub
Protected Sub Add(ByVal sender As Object, ByVal e As EventArgs)
Dim id As Integer = Convert.ToInt32(Request.QueryString("Id"))
Dim dt As DataTable = GetData(id)
If Session("Data") IsNot Nothing Then
Dim dx As DataTable = CType(Session("Data"), DataTable)
dx.Merge(dt)
Session("Data") = dx
gvCartItems.DataSource = dx
gvCartItems.DataBind()
Else
Session("Data") = dt
gvCartItems.DataSource = dt
gvCartItems.DataBind()
End If
End Sub
Protected Sub [Continue](ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("Products.aspx")
End Sub
Private Function GetData(ByVal id As Integer) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT ProductId,ProductName,UnitPrice FROM Products WHERE ProductID = @Id"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
cmd.Parameters.AddWithValue("@Id", id)
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot