Solved

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

Posted on 2008-06-13
3
2,992 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…

696 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