Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a small website where users upload names from excel sheet and those names get stored into a table. then i have a stored procedure that uses those names to extract data from other tables and output it into another excel sheet. i also have a gridview showing the names uploaded. my problem is that every time i upload new names the gridview either shows the previous names uploaded or is blank. when i run the stored procedure on sql the correct names show. i have been wracking my brain tryna figure out why the gridview doesnt show the latest names uploaded like on sql

What I have tried:

protected void Page_Load(object sender, EventArgs e)
    {
            SqlConnection con = new SqlConnection(strConnString);

            con.Open();

            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();
        }
Posted
Updated 12-Apr-19 0:14am
v2

Don't write all logic in this manner ever

1) Create saparate function for the logic
2) Manage page postback event
3) Call function from page postback event condition

C#
protected void Page_Load(object sender, EventArgs e)
{
              if (!IsPostBack)
                {
                   BindData();
                }


}




public void BindData(){

            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            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();
}


for further information

Page.IsPostBack Property (System.Web.UI) | Microsoft Docs[^]
 
Share this answer
 
v3
Comments
Member 14183767 11-Apr-19 5:23am    
hi thanks for ur input , my gridview is empty , but the tables have data
Nirav Prabtani 11-Apr-19 5:33am    
Have you updated the code as above ?

If yes then debug line by line and set try {} catch{} block for further information
Member 14183767 11-Apr-19 5:57am    
hi i added the try catch block, the gridview is still showing the old data
Member 14183767 11-Apr-19 6:19am    
is there no way i can clear my tables/gridview before a new sheet is uploaded and the stored procedure is called?
Nirav Prabtani 11-Apr-19 6:39am    
You can bind data after applying the order by time desc, this will return new records on top.

Are you using stored procedure for data binding purpose ?
Hi , thanks for the input!
i modified my stored procedures , now im able to clean the table before new sheet with names are uploaded
 
Share this answer
 

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