c# : Insert .rpt file into database.

Posted on 2009-04-20
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
    LVL 33

    Expert Comment

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

    Open in new window


    Author Comment

    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

    Yes. you will need the @ instead of ?.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now