[Last Call] Learn how to a build a cloud-first strategyRegister Now


c# : Insert .rpt file into database.

Posted on 2009-04-20
Medium Priority
Last Modified: 2012-05-06
I need to add a crystal report file to a database;
it could be Sql Server/Oracle.
I have the following code;
What am I doing wrong?
I am just trying this for the SQL server 2005 database;
string sFilePath = "C\\test.rpt";
//byte[] RptData = GetRPTData(sFullReportPath);
FileStream fs = new FileStream(sFilePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] FileData = br.ReadBytes((int)fs.Length);
SqlCommand cmd = new SqlCommand("INSERT into Tab1 (Creator,Contents ) values(?,?)",myConnection);
cmd.Parameters.AddWithValue("Creator", "Tom");                
 SqlParameter blob = new SqlParameter();
   // blob.DbType = DbType.Byte;
 blob.SqlDbType = SqlDbType.VarBinary;
 int i =  cmd.ExecuteNonQuery();
I get an error for cmd.Parameters.Add(RptData);
An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll

Additional information: The SqlParameterCollection only accepts non-null SqlParameter type objects, not Byte[] objects.
Question by:Enid_JP
  • 2
LVL 33

Expert Comment

ID: 24195976
Your file path is incorrect. You are missing the colon.
Use this instead.
string sFilePath = @"C:\test.rpt";

Open in new window


Author Comment

ID: 24201262
That was a typo when I edited the contents.
The real problem is that '?' is not accepted; the paremeter names have to be given in INSERT statement.
I got it working with the following code:
SqlCommand cmd = new SqlCommand("INSERT into Tab1 (CREATOR,CONTENTS) values(@CREATOR,@CONTENTS)", myConnection);
SqlParameter Creator = new SqlParameter("@CREATOR", SqlDbType.Char);
            Creator .Value = "USER1";
FileStream fs = new FileStream(sFullFilePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            byte[] FileData = br.ReadBytes((int)fs.Length);
SqlParameter Contents = new SqlParameter("@CONTENTS", SqlDbType.Image);
            Contents .Value = FileData ;
            cmd.Parameters.Add(Contents );
       int i =  cmd.ExecuteNonQuery();
This works only on SQL Server; when I use ODBCCommand this does not work; I think I will have to use '?' instead of parameter names in the INSERT T statement.
LVL 33

Accepted Solution

hongjun earned 200 total points
ID: 24201266
Yes. you will need the @ instead of ?.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

830 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