Click here to Skip to main content
15,886,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a two stored procedures,i have one that displays table data on a gridview (BenReport) on page load and another one (spTest) that extacts data from a table and i put that data in a dataset which then is extracted to excel ts using openofficexml . the problem is that the first row in my table is being skipped , i ran the stored procedure on sql and all the rows are returned. not sure why its skipping the first row

What I have tried:

here is my code:

protected void Page_Load(object sender, EventArgs e)
    {

        
        SqlConnection con = new SqlConnection(strConnString);

        SqlCommand command = new SqlCommand("BenReport", con) { CommandType = System.Data.CommandType.StoredProcedure };

        SqlDataAdapter sda = new SqlDataAdapter();


        command.Connection = con;
        sda.SelectCommand = command;

        DataSet ds = new DataSet();

        sda = new SqlDataAdapter("BenReport", con);

        sda.Fill(ds);

        GRDBencount.DataSource = ds.Tables[0];
        GRDBencount.DataBind();
        con.Close();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ExtractBen();
    }


    public void ExtractBen()
    {

        SqlConnection connex = new SqlConnection(strConnString);

        SqlCommand command = new SqlCommand("spTest", connex) { CommandType = System.Data.CommandType.StoredProcedure };

        SqlDataAdapter sda = new SqlDataAdapter();



        command.Connection = connex;
        sda.SelectCommand = command;
        command.CommandTimeout = 600;

        DataSet ds = new DataSet();





        sda = new SqlDataAdapter("spTest", connex);
        sda.Fill(ds);


        if (ds.Tables.Count > 0)
        {
            MemoryStream ms = new MemoryStream();
            int i = 1;
            using (ExcelPackage package = new ExcelPackage(ms))
            {
                foreach (DataTable table in ds.Tables)
                {


                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("sheet" + i++);
                    if (worksheet.Name == "sheet1")
                    { worksheet.Name = "Bencount"; }

                    if (worksheet.Name == "sheet2")
                    { worksheet.Name = "validateWages"; }

                    if (worksheet.Name == "sheet3")
                    { worksheet.Name = "BeneficiariestobeExctracted"; }

                    if (worksheet.Name == "sheet4")
                    { worksheet.Name = "Beneficiaries_with_0_Wdays"; }

                    if (worksheet.Name == "sheet5")
                    { worksheet.Name = "beneficiaries_with_wages_but_no_account_numbers"; }

                    if (worksheet.Name == "sheet6")
                    { worksheet.Name = "account_number>11_characters/wages<100"; }

                    if (worksheet.Name == "sheet7")
                    { worksheet.Name = " beneficiary_appear on more than project for the same month"; }

                    if(worksheet.Name == "sheet8")
                    { worksheet.Name = "account_number_shared_by_more_than_one_beneficiary"; }

                    if (worksheet.Name == "sheet9")
                    { worksheet.Name = "beneficiary_appear_on_more_than_project_for_the_same_month"; }

                    if (worksheet.Name == "sheet10")
                    { worksheet.Name = "Missing_Beneficiaries"; }


                    worksheet.Cells["A1"].LoadFromDataTable(table, true);
                    worksheet.Cells["A1:Z1"].AutoFitColumns();
                    worksheet.Cells["A1:Z1"].Style.Font.Bold = true;


                }
                Response.Clear();
                package.SaveAs(Response.OutputStream);
                Response.AddHeader("content-disposition", "attachchment; filename=BeneficiaryDetails.xls");

                Response.Charset = "";
                Response.ContentType = "application/vnd.xls";
                Response.End();
            }
        }

    }
Posted
Updated 29-Mar-19 4:14am
Comments
Vincent Maverick Durano 29-Mar-19 11:11am    
Which table are you having problem with? The BenReport or spTest? Also have you tried debugging your code by stepping into each line?
Member 14183767 1-Apr-19 3:19am    
yes i have a break point on page load that helps me debug line by line, both BenReport and spTest skip the first row in the table
Vincent Maverick Durano 1-Apr-19 15:19pm    
Are you saying the GRDBencount GridView doesn't show the first row record from your BenReport stored procedure?
Member 14183767 2-Apr-19 10:34am    
yes

1 solution

Copy the Complete ds as below, let me know if it works,



GRDBencount.DataSource = ds;


Thank you.
 
Share this answer
 
v2
Comments
Member 14183767 1-Apr-19 3:41am    
Hi , if i implement your solution it only shows one record

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900