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.
Some of my
previous articles are as follows: Bind
Class Property to GeidView Using C#.net In Asp.net, Gridview
Auto Generate Row Number In Asp.Net Using C#.Net, Bind
TextBox Control Inside TemplateField Of GridView Using C#.Net In Asp.Net, Short
GridView Data From Header Using C#.Net In Asp.Net, Export
GridView Or Table Data Into PDF By C#.Net In Asp.Net Using jQuery, Reading
XML Document in C# Using Linq and Bind To GridView In Asp.Net, How
to Find Gridview Control on Button Click in Asp.Net C#, Nested
GridView Using c#.Net In Asp.Net.
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.
0 comments:
Please let me know your view