| | |
not able read special charactered strings -EXCEL to DataSet-C#
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
Hi all,
my code is like..
and My EXCEL data is like
-------------------------------------------------------------------------------------
Gold --- in@GLDM.1 -- 14,917.00-- Gold MCX
--------------------------------------------------------------------------------------
ALUM--- in@ALUM.1 -- 630.00 -- Aluminium MCX
Im gettings values from row as 'Gold' 'EmptySpace' '14,917.00' 'EmptySpace'
I need to get 'in@GLDM.1' & 'Gold MCX' as it is from Excel sheet.
in myDataSet when check for 'in@ALUM.1' data, for that cell it is showing Empty.
Could you Please any body sovle it.
Thanks in Advance...
my code is like..
C# Syntax (Toggle Plain Text)
string strConn; strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\ifocus\\Excel Worksheet.xls;" + "Extended Properties=Excel 8.0;"; OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn); myDataSet = new DataSet(); myCommand.Fill(myDataSet, "ExcelInfo");
and My EXCEL data is like
-------------------------------------------------------------------------------------
Gold --- in@GLDM.1 -- 14,917.00-- Gold MCX
--------------------------------------------------------------------------------------
ALUM--- in@ALUM.1 -- 630.00 -- Aluminium MCX
Im gettings values from row as 'Gold' 'EmptySpace' '14,917.00' 'EmptySpace'
I need to get 'in@GLDM.1' & 'Gold MCX' as it is from Excel sheet.
in myDataSet when check for 'in@ALUM.1' data, for that cell it is showing Empty.
Could you Please any body sovle it.
Thanks in Advance...
Last edited by peter_budo; 26 Days Ago at 9:01 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
•
•
Join Date: Oct 2009
Posts: 31
Reputation:
Solved Threads: 3
-3
#2 26 Days Ago
I'm assuming its because the SQL is having trouble reading the 'Sheet1$.' Not sure if this will work but try and escape character:
In some languages special characters such as '$' will be a prefix for the start of a special case but putting escape characters before will make the language treat them as a literal.
C# Syntax (Toggle Plain Text)
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1\$\]", strConn);
In some languages special characters such as '$' will be a prefix for the start of a special case but putting escape characters before will make the language treat them as a literal.
•
•
Join Date: Nov 2009
Posts: 8
Reputation:
Solved Threads: 1
0
#6 25 Days Ago
Hi,
I tryied it with Form and I got the desired result.
I tryied it with Form and I got the desired result.
C# Syntax (Toggle Plain Text)
private void Form1_Load(object sender, EventArgs e) { OleDbConnection cnn = new OleDbConnection(); cnn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\temp\\myData.xls;" + "Extended Properties=Excel 8.0;"; OleDbDataAdapter dap = new OleDbDataAdapter("SELECT * From [Sheet1$]", cnn); DataSet ds = new DataSet(); dap.Fill(ds,"ExcelInfo"); foreach(DataRow d in ds.Tables[0].Rows) { MessageBox.Show(d[0].ToString()); MessageBox.Show(d[1].ToString()); MessageBox.Show(d[2].ToString()); MessageBox.Show(d[3].ToString()); }
0
#7 25 Days Ago
what did you change other than variable names?
One thing to watch out for with excel; if you have different data formats in a column it can cause problems. When reading in the data the majority datatype in each column is used. For example, if the first few rows of a column contain numbers, the column is assumed to contain numbers and any non-numeric values will be returned as NULL since they cant be converted to the columns datatype. I had a similar issue with a text column being interpretted as Double and returning nothing but DBNull's : /
Theres a nice breakdown here on how to handle the problem.
One thing to watch out for with excel; if you have different data formats in a column it can cause problems. When reading in the data the majority datatype in each column is used. For example, if the first few rows of a column contain numbers, the column is assumed to contain numbers and any non-numeric values will be returned as NULL since they cant be converted to the columns datatype. I had a similar issue with a text column being interpretted as Double and returning nothing but DBNull's : /
Theres a nice breakdown here on how to handle the problem.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
•
•
Join Date: Nov 2009
Posts: 8
Reputation:
Solved Threads: 1
-1
#8 25 Days Ago
•
•
•
•
what did you change other than variable names?
One thing to watch out for with excel; if you have different data formats in a column it can cause problems. When reading in the data the majority datatype in each column is used. For example, if the first few rows of a column contain numbers, the column is assumed to contain numbers and any non-numeric values will be returned as NULL since they cant be converted to the columns datatype. I had a similar issue with a text column being interpretted as Double and returning nothing but DBNull's : /
Theres a nice breakdown here on how to handle the problem.
Other then the variable name if you look at the code I changed the each column value to return as string and ATLEAST IT RETURNS WHAT WAS IN THE EXCEL SHEET.
0
#9 25 Days Ago
•
•
•
•
To explain you what I changed is very simple.
Other then the variable name if you look at the code I changed the each column value to return as string and ATLEAST IT RETURNS WHAT WAS IN THE EXCEL SHEET.
I saw that you added the Foreach loop but that wouldnt have altered what data was retrieved. You posted a query which you said returned empty values, then posted the same query with different variable names and said that it worked, i was jsut trying to figure out how.
When you said you were getting empty values, how were you checking the returned values initially? If you check them the same way now, do they still show as empty even when the .ToString() prints them correctly?
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
•
•
Join Date: Nov 2009
Posts: 8
Reputation:
Solved Threads: 1
0
#10 25 Days Ago
•
•
•
•
Im gonna assume that you hit CAPS by mistake rather than think you are being rude.
I saw that you added the Foreach loop but that wouldnt have altered what data was retrieved. You posted a query which you said returned empty values, then posted the same query with different variable names and said that it worked, i was jsut trying to figure out how.
When you said you were getting empty values, how were you checking the returned values initially? If you check them the same way now, do they still show as empty even when the .ToString() prints them correctly?
I am not the original one who posted the question of returning empty values. I am still learning C# and use this forum as a part of my learning curve. I tried the same problem with a webform and I got the desired result.
...and also I am not rude. I just wanted to convey my message and when you try to write while being on foot rather than chair it is bound to happen.
![]() |
Similar Threads
- Read excel file in asp.net (ASP.NET)
- How to read null-terminated strings from file? (C)
- Retriving data from excel sheet into an asp page (ASP)
- How to read/write to an MS-Excel file?? (C++)
- Read from a file then placing strings into a 2d array (C)
- Extract MS Excel Data embedded in MS Word (Visual Basic 4 / 5 / 6)
- Strings (C++)
- reading a line in excel (C)
Other Threads in the C# Forum
- Previous Thread: Using Interface and interface members in C#
- Next Thread: Creating a 3D desktop with C#
| Thread Tools | Search this Thread |
.net access ado.net algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client combobox control conversion csharp custom database databasesearch datagrid datagridview datagridviewcheckbox dataset datetime degrees development draganddrop drawing dynamiccreation encryption enum equation event excel file form format formatting forms function gdi+ httpwebrequest image index input install interface java label list listbox mandelbrot math mouse mouseclick mysql namevaluepairs operator path photoshop picturebox pixelinversion post powerpacks programming property radians regex remote remoting resource restore richtextbox server sleep socket sql statistics stream string table text textbox thread time timer update usercontrol validation visualstudio wait webbrowser windows winforms working wpf xml






