foreach loop only get last record to gridview c# asp.net

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();
                    }
                }
            }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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();
            }
        }

Method 2

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();
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x