connectseekers.com

Friday, 20 May 2016

Browse, Read and Populate or Show or Bind CSV File Data In GridView Using C#.Net in Asp.net

5/20/2016 - By Pranav Singh 0

This article will show you how you can browse or import, upload read or import and populate or display the imported data into the grid view control in asp.net using c#.net.

So for this article first we will create a new sample CSV file with header.


Now we will create a new asp.net application and add a file upload control, button control and a gridview control.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication7.WebForm3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Browse, Read and Populate/Show/Bind CSV File Data In GridView Using C#.Net in Asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Upload CSV:
            <asp:FileUpload ID="FileUpload1" runat="server" /><br />
            <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
        </div>

        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" />
                <asp:BoundField DataField="CountryName" HeaderText="Country Name" />
                <asp:BoundField DataField="Population" HeaderText="Population" />
                <asp:BoundField DataField="Code" HeaderText="Code" />
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>

Now we will create a CSVFile name folder or the folder name which you like.


Now add the below code into the .cs page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.IO;
namespace WebApplication7
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = GetCSVDataInDataTable();
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                Label1.Text = "Sorry not record found";
            }
        }
        private DataTable GetCSVDataInDataTable()
        {
            DataTable dt = new DataTable();
            if (FileUpload1.HasFile)
            {
                string filePath = "CSVFile/" + Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath(filePath));
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Path.GetDirectoryName(Server.MapPath(filePath)) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM " + Path.GetFileName(Server.MapPath(filePath)), conn);
                da.Fill(dt);
                conn.Close();
            }
            else
            {
                Label1.Text = "Sorry no file selected.";
            }
            return dt;
        }
    }
}

In above code check the GetCSVDataInDataTable method. In this method I have first upload the file into the CSVFolder and add after that I have read the csv file and added the imported data into the datatable.

Now check the page upload code. This piece code used to bind the imported data of CSV file into the gridview control. Now run the application and check the final output. Here is the datable value of csv file.

Press F5 to check he final output.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top