CsvDataReader(fileUpload <--- Error

117 views
Skip to first unread message

stewster

unread,
Sep 27, 2006, 10:47:15 AM9/27/06
to CSVChat
Hello,

I'm a developer evaluating a few .csv utilities for my company. So far
I've got everything working except the upload function. I'm using the
.Net example that is downloadable from you site.

The error is on this line of code.

Using loader As CsvDataReader = New
CsvDataReader(FileUpload.PostedFile.InputStream, Encoding.Default)

The error is:
Reference to a non-shared member requires an object reference.
- And FileUpload.PostedFile is underlined in red.

I'm testing your CSV reader in both VB.NET 2005 and ASP.NET
2.0(VB.NET)

Any ideas on how to fix this? Please let me know as soon as you can. If
all is well we will defiantly be purchasing this product.

shriop

unread,
Sep 27, 2006, 11:45:19 AM9/27/06
to CSVChat
No problem. It's a simple fix. If you look back at my code sample,
you'll see that my capitalization for FileUpload as you have it is
actually fileUpload, as in it's the name of the FileUpload control that
I have on my page. Since you're using the other case, you're
referencing the class directly rather than an instance of the class.
Just look back at your page and find what the name of the FileUpload
control actually is as it is on your page and substitute it's actual
name there instead of the class name and all should be well.

Bruce Dunwiddie

stewster

unread,
Sep 27, 2006, 1:17:09 PM9/27/06
to CSVChat
Wow! I feel like an idiot.. sorry! :-)

I have one more question if you don't mind. How can I get the headers
from a csv and put them in a list box?

My company gets several CSV files from companies that don't follow our
requirements on the CSV layout. So, they want me to put the headers for
both the csv file and the SQL database in two seperate listviews. And
then by selecting the header in the csv file and the header from the
sql database then click connect to set up the mappings for the csv and
sql and then upload the data.

blahh blahh - for you this may be supper easy task.. :-) but me.. just
look at my original post.. Need I say more? lol

Thank you for your time.

shriop

unread,
Sep 27, 2006, 2:29:19 PM9/27/06
to CSVChat
You'll have to save the file off somewhere so that it can live across
multiple web page responses, so like fileUpload.PostedFile.SaveAs can
save the file off to the local harddrive. You'll have to deal with
permissions issues and such to be able to do that. If it's a small
enough file, you can just save the file as a string, or a MemoryStream
into session memory. Once you get the file saved off somewhere, you'll
want to open it up with CsvReader, not CsvDataReader, and call the
ReadHeaders method. Then, call the Headers property, which returns you
a string array of all the names of the headers. You can then directly
bind the string array to a DropDownList's DataSource property and call
DataBind on the DropDownList. This will show you the names of the
headers in the DropDownList when the page is pulled up.

Bruce Dunwiddie

stewster

unread,
Sep 28, 2006, 4:21:30 PM9/28/06
to CSVChat
I have everything setup but I'm getting an error that does not make
sense to me. the error is:

The given ColumnName 'BARCODE' does not match up with any column in
data source.

however the sql database that I'm uploading to does have the column
name BARCODE

Below is an example of the code I'm using:

Using loader As CsvDataReader = New

CsvDataReader(FileUpload1.PostedFile.InputStream, Encoding.Default)
loader.HasHeaders = True

loader.Columns.Add("nvarchar")
loader.Columns.Add("nvarchar")
loader.Columns.Add("nvarchar")
loader.Columns.Add("nvarchar")
loader.Columns.Add("nvarchar")
loader.Columns.Add("nvarchar")
loader.Columns.Add("nvarchar")
loader.Columns.Add("nvarchar")
loader.Columns.Add("nvarchar")
loader.Columns.Add("nvarchar")

Using bulkCopy As Data.SqlClient.SqlBulkCopy = New
Data.SqlClient.SqlBulkCopy("Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated
Security=True;User Instance=True")
bulkCopy.DestinationTableName = "tblRecall"

' mappings required because we're skipping the
customer_id column

bulkCopy.ColumnMappings.Add(ListMapCSV.Items(0).ToString,
ListSQL.Items(0).ToString)

bulkCopy.ColumnMappings.Add(ListMapCSV.Items(1).ToString,
ListSQL.Items(1).ToString)

bulkCopy.ColumnMappings.Add(ListMapCSV.Items(2).ToString,
ListSQL.Items(2).ToString)

bulkCopy.ColumnMappings.Add(ListMapCSV.Items(3).ToString,
ListSQL.Items(3).ToString)

bulkCopy.ColumnMappings.Add(ListMapCSV.Items(4).ToString,
ListSQL.Items(4).ToString)
bulkCopy.ColumnMappings.Add(ListMapCSV.Items(5).ToString,
ListSQL.Items(5).ToString)

bulkCopy.ColumnMappings.Add(ListMapCSV.Items(6).ToString,
ListSQL.Items(6).ToString)
bulkCopy.ColumnMappings.Add(ListMapCSV.Items(7).ToString,
ListSQL.Items(7).ToString)
bulkCopy.ColumnMappings.Add(ListMapCSV.Items(8).ToString,
ListSQL.Items(8).ToString)

bulkCopy.ColumnMappings.Add(ListMapCSV.Items(9).ToString,
ListSQL.Items(9).ToString)
bulkCopy.ColumnMappings.Add(ListMapCSV.Items(10).ToString,
ListSQL.Items(10).ToString)

'bulkCopy.ColumnMappings.Add(ListMapCSV.Items(11).ToString,
ListSQL.Items(11).ToString)

'bulkCopy.ColumnMappings.Add(ListMapCSV.Items(12).ToString,
ListSQL.Items(12).ToString)

'bulkCopy.ColumnMappings.Add(ListMapCSV.Items(13).ToString,
ListSQL.Items(13).ToString)

'bulkCopy.ColumnMappings.Add(ListMapCSV.Items(14).ToString,
ListSQL.Items(14).ToString)

bulkCopy.WriteToServer(loader)
End Using
End Using

shriop

unread,
Sep 28, 2006, 4:41:23 PM9/28/06
to CSVChat
"does not match up with any column in data source." literally means
source, as in the file that is being uploaded. The database table in
this context would be referred to as the destination. The code the way
you have it is going to name the source columns based on the actual
name of the column in the header record, including being case
sensitive. I'm currently making the bet that the file you're uploading
does not have a header called BARCODE, especially with the same case.
If you still don't see the issue, go ahead and email me an example
source file along with the destination table definition directly to me
at br...@csvreader.com and I'll track down where the mismatch is taking
place. I'm sure it's something just slightly off.

Bruce Dunwiddie

shriop

unread,
Sep 29, 2006, 12:42:30 AM9/29/06
to CSVChat
Long story short, for anyone that maybe be watching this thread, Larry
stumbled into a bug where if there are more headers in the source file
than columns in the column collection, an internal index out of bounds
exception is thrown. I've rolled the fix up into the current production
source branch, written regression tests so the bug can never come back,
and given Larry a free $75 license for his troubles.

Bruce Dunwiddie

Reply all
Reply to author
Forward
0 new messages