Retrieve the data from excel row by row randomly or sequentially and need to display in ASP.net c#

Ashok kumar S 1 Reputation point
2022-08-04T05:38:12.243+00:00

I want to Retrieve the data from excel row by row randomly or sequentially and need to display it in ASP.net c# web page

I tried and done to display gridview data but not individual data. Kindly help me

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,307 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 25,876 Reputation points Microsoft Vendor
    2022-08-04T09:12:27.193+00:00

    Hi @Ashok kumar S ,
    https://learn.microsoft.com/en-us/answers/questions/954594/retrieve-the-data-from-oracle-database-row-by-row.html
    I see you asked two questions, do you want to achieve retrieving data from excel or oracle database?

    I tried and done to display gridview data but not individual data.

    individual data? What does individual data refer to, you don't want to do it through gridview? Can you describe in more detail?
    I wrote an example, hope it helps you.

    <asp:DropDownList ID="dropdown1" runat="server" OnSelectedIndexChanged="ddlSlno_SelectedIndexChanged"   AutoPostBack="true" AppendDataBoundItems="True">    
                   <asp:ListItem Selected="True" Value="Choose">- Choose -</asp:ListItem>    
               </asp:DropDownList>    
               <asp:GridView ID="Grid1" runat="server">    
               </asp:GridView>    
               <asp:Label ID="lbl1" runat="server" />   
    

      OleDbConnection x;  
            protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!IsPostBack)  
                {  
                    GenerateExcelData("Choose");  
                }  
            }  
            protected void ddlSlno_SelectedIndexChanged(object sender, EventArgs e)  
            {  
                GenerateExcelData(dropdown1.SelectedValue);  
            }  
      
            private void GenerateExcelData(string SlnoAbbreviation)  
            {  
                try  
                {  
                    string read = Path.GetFullPath(Server.MapPath("~/empdetail.xlsx"));  
      
                    if (Path.GetExtension(read) == ".xls")  
                    {  
                        x = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + read + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");  
                    }  
                    else if (Path.GetExtension(read) == ".xlsx")  
                    {  
                        x = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + read + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");  
                    }  
                    x.Open();  
                    OleDbCommand y = new OleDbCommand();  
                    OleDbDataAdapter z = new OleDbDataAdapter();  
                    DataSet dset = new DataSet();  
                    y.Connection = x;  
                    y.CommandType = CommandType.Text;  
                    y.CommandText = " SEL ECT distinct([Slno]) FROM [Sheet1$]";  
                    z = new OleDbDataAdapter(y);  
                    z.Fill(dset, "Slno");  
                   dropdown1.DataSource = dset.Tables["Slno"].DefaultView;  
                   if (!IsPostBack)  
                   {  
                    dropdown1.DataTextField = "Slno";  
                    dropdown1.DataValueField = "Slno";  
                    dropdown1.DataBind();  
                   }  
                  if (!String.IsNullOrEmpty(SlnoAbbreviation) && SlnoAbbreviation != "Choose")  
                   {  
                       y.CommandText = " S E L E C T *  FROM [ She et1 $ ] w he re Slno= @ Slno_Abbreviation";  
                       y.Parameters.AddWithValue("@Slno_Abbreviation", SlnoAbbreviation);  
                   }  
                else  
                 {  
                    y.CommandText = "SEL ECT * FROM [ Sheet1 $ ]";  
                 }  
                z = new OleDbDataAdapter(y);  
                z.Fill(dset);  
                  Grid1.DataSource = dset.Tables[1].DefaultView;  
                  Grid1.DataBind();  
                
                }  
                catch (Exception ex)  
                {  
                    lbl1.Text = ex.ToString();  
                }  
                finally  
                {  
                    x.Close();  
                }  
            }  
    

    228040-test1.gif
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments