Reputation: 3
Hi want to fetch data from db using string builder which i am getting from previous page in session . But when o try to bind data i only get last data in the gridview. Please help
Below code is how i get multiple values from checkbox from gridview and pass it to next page using string builder and session.
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
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[7].Text ).AppendLine();
strb.Append(GridView1.Rows[i].Cells[7].Text).AppendLine();
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select items to continue');", true);
}
}
// Session["vendor"] = strb.ToString();
Session["vendor"] = strb.ToString().Trim('\n');
Response.Redirect("order.aspx");
}
}
in page 2 i use that session to get values and bind in gridview. The problem is only get last value from the string in foreach loop.
if (!this.IsPostBack)
{
if (Session["vendor"] != null)
{
string[] vendors = Session["vendor"].ToString().Split('\n');
foreach (string vendor in vendors)
{
var data = vendor.Trim();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
sqlCommand cmd = new SqlCommand("select [Part number],Nomenclature,quantity,[quantity available],[unit price] from Catalouge where [Vendor Code]=('" + data + "')", conn);
conn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
}
}
Upvotes: 0
Views: 464
Reputation: 10870
You are binding your grid for each vendor code again and again.
Try this
if (!this.IsPostBack)
{
if (Session["vendor"] != null)
{
string[] vendors = Session["vendor"].ToString().Split('\n');
var data = string.Join(", ", vendors.Select(v => $"'{v}'"));
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
sqlCommand cmd = new SqlCommand("select [Part number],Nomenclature,quantity,[quantity available],[unit price] from Catalouge where [Vendor Code] in (" + data + ")", conn);
conn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
}
Upvotes: 0
Reputation: 1878
you are looping through each vendor and overriding the Grid data. Instead, you can get data for all the vendors at a time and bind data to Grid.
Please refer the below logic
if (!this.IsPostBack)
{
if (Session["vendor"] != null)
{
string[] vendors = Session["vendor"].ToString().Split('\n');
string all_vendors = string.Join("','", vendors).Replace(" ", "");
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
sqlCommand cmd = new SqlCommand("select [Part number],Nomenclature,quantity,[quantity available],[unit price] from Catalouge where [Vendor Code] IN ('" + all_vendors + "')", conn);
conn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
}
Upvotes: 2