This
article will show you how you can browse or import, upload read or import and
populate or display the imported data into the DataList 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, File Upload and Displaying Them as Thumbnails
in DataList in MVC3 By Using Razor, C#.Net, Bind Data In DataList Using C#.Net and VB.Net
in Asp.Net.
Now we will
create a new asp.net application and add a file upload control, button control
and a DataList 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>Read and Show CSV
File Data In DataList 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:DataList ID="DataList1"
runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
<ItemStyle BorderWidth="1px" />
<ItemTemplate>
<table width="90%">
<tr>
<td style="text-align: right; font-weight: bold;">Name: </td>
<td style="text-align: left"><%#Eval("CountryName")
%></td>
</tr>
<tr>
<td style="text-align: right; font-weight: bold;">Population:
</td>
<td style="text-align: left"><%#Eval("Population")
%></td>
</tr>
<tr>
<td style="text-align: right; font-weight: bold;">Code</td>
<td style="text-align: left"><%#Eval("Code") %></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
|
Here in this code we will display data Horizontally and each
row contain 2 items to display. For you can change the count by following below
detail.
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)
{
DataList1.DataSource = dt;
DataList1.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 DataList control. Now run the application and check the final
output. Here is the datable value of csv file.
0 comments:
Please let me know your view