Skip to main content

How to store Excel data in Gridview

Import System.Data.OleDb(using System.Data.OleDb)in code behind file

Then in button onclick event write,

OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=D:\status.xls;Extended Properties=Excel 8.0");
OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);
OleDbDataReader dr;

connection.Open();
dr = command.ExecuteReader(CommandBehavior.CloseConnection);

DataTable excelData = new DataTable("ExcelData");
excelData.Load(dr);
GridView1.DataSource = excelData;
GridView1.DataBind();

Comments

Popular posts from this blog

Difference between two Dates in C#.NET ?

To find the difference between two dates is very simple in VB — By using DateDiff method. But in C#, there is no direct method to do so. but there is a way to achive this. For this we need to understand TimeSpan Class. The following code snippet will show you how to find the difference. DateTime startTime = DateTime.Now; DateTime endTime = DateTime.Now.AddSeconds( 75 ); TimeSpan span = endTime.Subtract ( startTime ); Console.WriteLine( “Time Difference (seconds): ” + span.Seconds ); Console.WriteLine( “Time Difference (minutes): ” + span.Minutes ); Console.WriteLine( “Time Difference (hours): ” + span.Hours ); Console.WriteLine( “Time Difference (days): ” + span.Days ); By concatenating all this you will get the difference between two dates. You can also use span.Duration(). There are certain limitations to. The TimeSpan is capable of returning difference interms of Days, Hours, mins and seconds only. It is not having a property to show difference interms of Months and years. Hope this...

.NET Reflection

Reflection is the feature in .Net, which enables us to get some information about object in runtime. That information contains data of the class. Also it can get the names of the methods that are inside the class and constructors of that object. To write a C# .Net program which uses reflection, the program should use the namespace System.Reflection. To get type of the object, the typeof operator can be used. There is one more method GetType(). This also can be used for retrieving the type information of a class. The Operator typeof allow us to get class name of our object and GetType() method uses to get data about object?s type. Example program: namespace Reflection { public class TestDataType { public TestDataType() { counter = 1; } public TestDataType(int c) { counter = c; } private int counter; public int Inc() { return counter++; } public int Dec() ...

How to edit GridView manually

Filling, Editing, Updating, Deleting from a GridView is quite easy now. Just follow these steps. 1. Open Visual Studio create a web based project. 2. Go to design view of Default.aspx page and then drag&drop a GridView control from standard Visual Studio Toolbox. 3.Right click on GridView goto properties and then disable AutoGeneratedColumns property to false. 4. Navigate to your source page (default.aspx). then we will found the tags look like asp:gridview id="GridView1" runat="server" autogeneratecolumns="False"> 5. Now, open tab in gridview and wirte the following code. 6. Save the File. Now goto your codebehind file(Default.aspx.cs) 7. Replace your Page_Load event with the following protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { fillgrid(); } } 8. And write the Grid method private void fillgrid() { con.Open(); da = new SqlDataAdapter("select * from login", con...