suhaas121 says:
string
[] vendors = Session[
"vendor"
].ToString().Split(
'\n'
);
Loop through the string array to get each row details.
Check this example. 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
Home
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnadd" OnClick="btnadd_Click" runat="server" Text="Add To Cart" />
Default
<asp:GridView runat="server" ID="GridView1"></asp:GridView>
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
C#
Home
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
StringBuilder strb = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if (isChecked)
{
strb.Append(GridView1.Rows[i].Cells[1].Text).AppendLine();
}
}
Session["vendor"] = strb.ToString().Trim();
Response.Redirect("Default.aspx");
}
Default
protected void Page_Load(object sender, EventArgs e)
{
if (Session["vendor"] != null)
{
string[] vendors = Session["vendor"].ToString().Split('\n');
DataSet ds = new DataSet();
foreach (string vendor in vendors)
{
var data = vendor.Trim();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
SqlCommand cmd = new SqlCommand("select FirstName+' '+LastName Name,Country from Employees where EmployeeID='" + data + "'", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
if (ds.Tables.Count > 0)
{
ds.Tables[0].Merge(dt);
}
else
{
ds.Tables.Add(dt);
}
}
}
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
VB.Net
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub btnadd_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim strb As StringBuilder = New StringBuilder()
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
Dim isChecked As Boolean = (CType(row.FindControl("CheckBox1"), CheckBox)).Checked
If isChecked Then
strb.Append(GridView1.Rows(i).Cells(1).Text).AppendLine()
End If
Next
Session("vendor") = strb.ToString().Trim()
Response.Redirect("Default.aspx")
End Sub
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Session("vendor") IsNot Nothing Then
Dim vendors As String() = Session("vendor").ToString().Split(vbLf)
Dim ds As DataSet = New DataSet()
For Each vendor As String In vendors
Dim data = vendor.Trim()
Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("connString").ConnectionString)
Dim cmd As SqlCommand = New SqlCommand("select FirstName+' '+LastName Name,Country from Employees where EmployeeID='" & data & "'", conn)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
If dt.Rows.Count > 0 Then
If ds.Tables.Count > 0 Then
ds.Tables(0).Merge(dt)
Else
ds.Tables.Add(dt)
End If
End If
Next
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
End If
End Sub
Screenshot
![](https://i.imgur.com/6UEWZcX.gif)