Link to home
Start Free TrialLog in
Avatar of Adnan
AdnanFlag for Norway

asked on

How can i insert a CrystalReport rpt. file to database....

Hi

I have a simale form where i can browse and select the rpt. file, and i want to insert the file into my db table....!
I have added pic of my form, where i have a textfiel, a browse button, and a upload button to load rpt. file and insert it into db...can anyon guide me how i can do that?
private void button1_Click(object sender, EventArgs e)
        {
            ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load(txtCrystalReport.Text);
            //crystalReportViewer1.ReportSource = cryRpt;
            //crystalReportViewer1.Refresh();

        }

Open in new window

crystalreport.png
Avatar of Reza Rad
Reza Rad
Flag of New Zealand image

you can upload any file you want in sql server table, with field type IMAGE,
the scenario for all files is same, you should convert it to binary and save it.

this sample is for save a picture file to sql server, but you should do similar scenario:
http://shabdar.org/store-save-images-in-sql-server.html
Avatar of Adnan

ASKER

oki, but why should i convert it to binary and save it?
because image datatype in sql server will save binary data inside
did you tried the link i suggested?
Avatar of Adnan

ASKER

yes i trued the link you suggested, and the code is working fine, sql is being executed but table column is still null and is not being updated......se the code below:
string sql = "UPDATE Setting SET Uploadfile = (convert (binary(32),@CrystalRepByteArr)) WHERE Setting_ID = 3";
                string signup = conn;
                SqlConnection myConnection = new SqlConnection(signup);
                myConnection.Open();
                SqlCommand cmd = new SqlCommand(sql, myConnection);
                
                IDbDataAdapter ad = GetDataAccess().GetNewDataAdapter(myDataAccess.GetNewCommand(sql));
               // cmd.Parameters.AddWithValue("CrystalRepByteArr", "@CrystalRepByteArr");
                IDataParameter param = GetDataAccess().AddParameter(cmd, "@CrystalRepByteArr", Adra.Data.DbType.VarBinary);
                param.Value = CrystalRepByteArr;
                
                int i = cmd.ExecuteNonQuery();

Open in new window

Avatar of Adnan

ASKER

i changed my code to , se the code below....the same is happening....the sql query is being executed if check sql server profiler....but my column is still null and its not being updated....
string sql = "UPDATE Setting SET Uploadfile = (convert (binary(32),@CrystalRepByteArr)) WHERE Setting_ID = 3";
                IDbDataAdapter ad = GetDataAccess().GetNewDataAdapter(myDataAccess.GetNewCommand(sql));
                IDbCommand idcmd = GetDataAccess().GetNewCommand(sql);
                IDataParameter param = GetDataAccess().AddParameter(idcmd, "@CrystalRepByteArr", Adra.Data.DbType.VarBinary);
                param.Value = CrystalRepByteArr;

                GetDataAccess().ExecuteSql(idcmd);

Open in new window

did you fill the CrystalRepByteArr correctly?
does it has any value after filling?
could you paste the code which you fill this variable?


Avatar of Adnan

ASKER

yes, it has a value in bytes....se the code below...
string filePath = txtCrystalReport.Text;  

            byte[] RptData = File.ReadAllBytes(filePath);
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            byte[] FileData = br.ReadBytes((int)fs.Length);

Open in new window

Avatar of Adnan

ASKER

filedata is the CrystalRepByteArr
ok,
and another thought is that
could you try to put this query in a stored procedure and just execute it from .net code:

UPDATE Setting SET Uploadfile =@CrystalRepByteArr WHERE Setting_ID = 3

note that declare @crystalRepByteArr as image.


and let me know what is datatype of Uploadfile field?
Avatar of Adnan

ASKER

oki i will trie to put it in a stored procedure...., the data type of Uploadfile field is varbinary(max)
change the datatype of Uploadfile to IMAGE , and try
ASKER CERTIFIED SOLUTION
Avatar of Adnan
Adnan
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial