Solved

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

Posted on 2008-06-13
3
2,989 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
ID: 21776730
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
ID: 21776945
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
ID: 31466847
Your post helped me to do the task much faster.Anyway i did this in c#, not VB.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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