473,763 Members | 3,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trouble parsing Excel file to byte array

I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through the application's front end the file appears to be corrupt. The front-end application has a way of inserting files to the column, however when I analyze this in SQL Profiler the contents of the Byte array appear to be quite different than the one my code is generating. I'm not sure if this is an ADO.NET issue or a problem with the way I'm parsing the XLS file into the Byte array.

I've been banging my head on this one for a couple days now, any help would be much appreciated

Thanks

Andr
public static void AddFile(string sAccountKey

tr

FileStream fs = new FileStream(sFil ePath, FileMode.Open, FileAccess.Read )
byte[] file = new byte[fs.Length]
fs.Read(file, 0, file.Length)
fs.Close();
string sDescription = "900 Special Billing Invoice " + Menu.datEndDate .ToShortDateStr ing()
SqlConnection cn = new SqlConnection(M enu.sConnect)
SqlCommand cmd = cn.CreateComman d()
cmd = cn.CreateComman d()
cn.Open()
cmd.CommandText = sSQL
cmd.Parameters. Add("@AccountKe y", SqlDbType.Int, 4)
cmd.Parameters["@AccountKe y"].Value = myReportDS.Acco unt[0].AccountKey
cmd.Parameters. Add("@Descripti on", SqlDbType.VarCh ar, 50).Value = sDescription
cmd.Parameters. Add("@ObjectTyp e", SqlDbType.Image , file.Length).Va lue = file
string sSQL = "INSERT INTO tblSupportAccou ntsOLEObjects (AccountKey, Description, ObjectType) "
"VALUES (@AccountKey, @Description, @ObjectType);"
cmd.CommandText = sSQL
cmd.CommandTime out = 0
cmd.ExecuteNonQ uery()
cn.Close();

catch (Exception ex

MessageBox.Show ("Exception at Print900.AddFil e(string sAccountKey)\r\ n" + ex.Source + "\r\n" + ex.Message + "\r\n" + ex.StackTrace, "An error has occured.", MessageBoxButto ns.OK, MessageBoxIcon. Error)
return

}
Nov 16 '05 #1
1 13294
I tried out this code

FileStream input = new FileStream("C:\ \test.xls", FileMode.Open,
FileAccess.Read );
byte[] bytes = new byte[input.Length];
input.Read(byte s, 0, bytes.Length);
input.Close();

FileStream output = new FileStream("C:\ \test2.xls", FileMode.Create ,
FileAccess.Writ e);
output.Write(by tes, 0, bytes.Length);
output.Close();

and test2.xls opened just fine in Excel so I think your byte array is
alright. Maybe you should try reading the bytes out of SQL Server and write
them to disk and see if it opens in Excel.
"Andre Ranieri" <an*******@disc ussions.microso ft.com> wrote in message
news:E6******** *************** ***********@mic rosoft.com...
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work
ok, but when I attempt to access the file through the application's front
end the file appears to be corrupt. The front-end application has a way of
inserting files to the column, however when I analyze this in SQL Profiler
the contents of the Byte array appear to be quite different than the one my
code is generating. I'm not sure if this is an ADO.NET issue or a problem
with the way I'm parsing the XLS file into the Byte array.
I've been banging my head on this one for a couple days now, any help would be much appreciated.
Thanks,

Andre

public static void AddFile(string sAccountKey)
{
try
{
FileStream fs = new FileStream(sFil ePath, FileMode.Open, FileAccess.Read );
byte[] file = new byte[fs.Length];
fs.Read(file, 0, file.Length);
fs.Close();
string sDescription = "900 Special Billing Invoice " + Menu.datEndDate .ToShortDateStr ing(); SqlConnection cn = new SqlConnection(M enu.sConnect);
SqlCommand cmd = cn.CreateComman d();
cmd = cn.CreateComman d();
cn.Open();
cmd.CommandText = sSQL;
cmd.Parameters. Add("@AccountKe y", SqlDbType.Int, 4);
cmd.Parameters["@AccountKe y"].Value = myReportDS.Acco unt[0].AccountKey;
cmd.Parameters. Add("@Descripti on", SqlDbType.VarCh ar, 50).Value = sDescription; cmd.Parameters. Add("@ObjectTyp e", SqlDbType.Image , file.Length).Va lue = file; string sSQL = "INSERT INTO tblSupportAccou ntsOLEObjects (AccountKey, Description, ObjectType) " + "VALUES (@AccountKey, @Description, @ObjectType);";
cmd.CommandText = sSQL;
cmd.CommandTime out = 0;
cmd.ExecuteNonQ uery();
cn.Close();
}
catch (Exception ex)
{
MessageBox.Show ("Exception at Print900.AddFil e(string sAccountKey)\r\ n" + ex.Source + "\r\n" + ex.Message + "\r\n" + ex.StackTrace, "An error has
occured.", MessageBoxButto ns.OK, MessageBoxIcon. Error); return;
}
}

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8560
by: Phil Powell | last post by:
What would one best recommend to parse an existing Excel spreadsheet (was done in Excel 97 or 2000 not sure to be honest)? I am looking for the most practical way of parsing an existing spreadsheet to place contents into a mySQL db. Thanx, Phil PS: can you use fopen() to read the contents and parse? The spreadsheet is on a remote site.
2
4347
by: Sathyaish | last post by:
I am using MCI (winmm.dll) to read, record and playback sound. For now, I am doing this with disk files instead of realtime doing it straight from the memory. If I want to stream/relay/transmit this sound (file) on a Windows socket (not using FTP but TCP), can I read the file into a byte array? Because if it is possible then I can send it on the socket but the problems I foresee are: How will I repack it into the file. Will just reading...
3
12309
by: Lee Crabtree | last post by:
I have a managed DLL that I've used to expose a C++ class. One of the functions in this class reads a line out of a file into a buffer of type "unsigned char*". Since all the data is just straight byte data (that is, I'm concerned with individual bits packed into the bytes), I'd like to put that data into a byte array that's passed from the C# app to the DLL function. In the past, I've been able to marshal strings to character strings...
20
3529
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int *)a2; // now a1=a2, a1=a2, etc.
0
1951
by: shaurya.rastogi | last post by:
I need to open an Excel file that has been stored in the Access Database using the insert Object functionality of MSAccess manually. What i am aware of is that i cant just read the field containing the Excel File into a Byte Array and pass it to the Excel object in C#,as the file is wrapped in the OLE Wrapper used by Access while inserting the file in database. I have tried locating the Header of Excel file from the byte array and...
0
1388
by: shaurya | last post by:
I need to open an Excel file that has been stored in the Access Database using the insert Object functionality of MSAccess manually. What i am aware of is that i cant just read the field containing the Excel File into a Byte Array and pass it to the Excel object in C#,as the file is wrapped in the OLE Wrapper used by Access while inserting the file in database . I have tried locating the Header of Excel file from the byte array and read...
5
21240
by: Aneesh Pulukkul[MCSD.Net] | last post by:
How to convert a "Non Serializable" object to byte array. The object is a dynamically created Excel workbook. As per my understanding an object can be written and read from a stream Only if it's serialized. Any ideas? Thanks in advance.
15
9439
by: patf | last post by:
Hi - experienced programmer but this is my first Python program. This URL will retrieve an excel spreadsheet containing (that day's) msci stock index returns. http://www.mscibarra.com/webapp/indexperf/excel?priceLevel=0&scope=0&currency=15&style=C&size=36&market=1897&asOf=Jul+25%2C+2008&export=Excel_IEIPerfRegional Want to write python to download and save the file. So far I've arrived at this:
1
7729
by: bharath652 | last post by:
Hi, I need to know how to get the Mime type of a Document from the Byte array. I'm not having file name of file extension. I have only Byte array of a document. To be more specific, Using the binary of a docuement, I need to find whether it is MS Word or MS Excel and so on. Please tell me is there a way to find it in Java. Thanks in advance
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.