Solved

Import from Excel to datagrid using c#.-- URGENT.

Posted on 2008-06-13
3
2,987 Views
Last Modified: 2010-04-21
hi,

    I have a datagrid with about 10 columns in it.Now i want to import the values in excel file to datagrid.

scenario :

1-----There are only 5 columns in excel.So i want to match the column names for excel and datagrid for populating the same column values in datagrid.
2-----I have a session value session["RiskID"]. I want to check this value against all the values in first column"RiskId" in excel. If any one value in the column dosen't match, then it shud not import to datagrid.Instead it shud prompt a message to user.
3-----I have a column called "SI" in excel. I want to check all the values in this column. If any one value in the column has "0", then it shud not import to datagrid.Instead it shud prompt a message to user.
4-----If any values in excel has values like " Microsoft's " with an apostaphe, it shud not be a problem in importing the values to datagrid.

Please help me on this. Thanks. Its very Urgent
0
Comment
Question by:srk1982
3 Comments
 
LVL 1

Accepted Solution

by:
ron10023 earned 250 total points
Comment Utility
Hi srk1982,

First of all, you should never use signs like apostaphe in excel because the structure of an excel file is basically xml and we all know that xml doesn't handle those signs well.

After that said, just go to www.connectionstrings.com and take a connection string for excel. This way you can use the excel file just like a database and use an sql query for all your needs.

Hope it helped

Ron
0
 
LVL 14

Assisted Solution

by:Dustin Hopkins
Dustin Hopkins earned 250 total points
Comment Utility
Ok lets take a swing...
1---Just set your datagrid not to load the columns automatically, instead add them as bound columns and set the datafield to the column name in excel.
2---not completely sure what you mean by not loading, you may want to change your select statement to filter your results or you can loop throught he datagrid and change/edit text.
4---There shouldn't be a problem with apostophes, though it is not prefered usage in excel documents

'Code to query excel

Dim sExcelFileName As String = "~\current.xls"

        Dim sWorkbook As String = "[DSRO$]"

        Dim sExcelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath(sExcelFileName) & ";Extended Properties=""Excel 8.0;HDR=YES;"""

        Dim OleDbConn As OleDbConnection = New OleDbConnection(sExcelConnectionString)

        Dim OleDbCmd As OleDbCommand = New OleDbCommand(("SELECT * FROM " & sWorkbook), OleDbConn)

        OleDbConn.Open()

        Dim dr As OleDbDataReader = OleDbCmd.ExecuteReader()

        dg.DataSource = dr

        dg.DataBind()

        OleDbConn.Close()

Open in new window

0
 

Author Closing Comment

by:srk1982
Comment Utility
Your post helped me to do the task much faster.Anyway i did this in c#, not VB.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now