Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 4th, 2003, 06:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default Insert Image to MS-SQL

Hi there:
Im going to insert an Image into MS-SQL Server 2000, could anyone give me a sample!

I use the code that I've useing for other tables but I guess I need something more for inserting Images to Database!
Bye the way Im using DataSet & DataAdapter.

Any help is appreciated.

Always:),
Hovik Melkomian.
__________________
Always,
Hovik Melkomian.
 
Old October 11th, 2003, 12:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

I found my amswer guys!!!

Always:),
Hovik Melkomian.
 
Old October 30th, 2003, 10:20 PM
Registered User
 
Join Date: Oct 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi melvik,

I trying to do the same: inserting an image into MS-SQL.
Can you show me your sample code?

Yeah, i follow the instructions on
http://www.developerfusion.com/show/3905/1/

But I stumble on it. Here is the error message that I got:
-------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 68: imgStream = UploadFile.PostedFile.InputStream;
-----------------------------------------------------------------

I can't figure what I miss. Can anyone help?

Thanks.


 
Old October 30th, 2003, 10:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I am new to C# also but I think you need to use the datatype "Stream".

Stream imgStream = UploadFile.PostedFile.InputStream;






======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old November 1st, 2003, 12:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

All r the same as u do for ur text fields but Image part! Here u will just need to change the format of Image to Bitmap Object.
Anyway its what I've done for adding a pic to my dataset using DataAdapter & StoredProcedure on SQL-Server 2000.

Code:
        private void buttonScan_Click(object sender, System.EventArgs e)
        {
            if( DialogResult.OK == FileOpenDlg.ShowDialog() )
            {
                string file = FileOpenDlg.FileName;
                System.IO.FileStream stream = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, (int)stream.Length);
                stream.Close();
                string strName = System.IO.Path.GetFileNameWithoutExtension(file);

                //SavePic
                DataRow PicRow;
                PicRow = dsDabir1.Tables["Images"].NewRow();
                PicRow["IId"] = 22;
                PicRow["ImgPage"] = 1;
                PicRow["ImgPic"] = buffer;
                dsDabir1.Tables["Images"].Rows.Add(PicRow);
                sqlDataAdapterPix.Update(dsDabir1, "Images");
                buffer = null;

                //ShowPic
            }//if
        }
Always:),
Hovik Melkomian.
 
Old November 3rd, 2003, 09:13 PM
Registered User
 
Join Date: Nov 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to uponline_aom
Default

 Up

 
Old November 9th, 2003, 01:56 AM
Registered User
 
Join Date: Oct 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Melvik,

I got it now.
Thanks for your help.

Vivi

 
Old November 9th, 2003, 02:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

pleasure 2 be useful.

Always:),
Hovik Melkomian.
 
Old June 15th, 2006, 09:04 AM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by rodmcleay
 I am new to C# also but I think you need to use the datatype "Stream".

Stream imgStream = UploadFile.PostedFile.InputStream;






======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
I'm running into the same type of problem but I do have my code with the Stream in it.

I'm getting a System.NullReferenceException: Object reference not set to an instance of an object.

I'd put all my code here but you guys would probably die trying to read it all. I did use the reference link to get my code (http://www.developerfusion.com/show/3905/5/) and even though I used that code I'm still getting the error.

Here's my click function (and yes my item is named BtnUpload not UploadBtn

any suggestions??
        public void BtnUpload_Click(object sender, EventArgs e)
        {
            if (Page.IsValid) //Save Image
            {
                Stream imgStream = UploadFile.PostedFile.InputStream;
                int imgLen = UploadFile.PostedFile.ContentLength;
                string imgContentType = UploadFile.PostedFile.ContentType;
                string imgName = txtAdCode.Value;
                byte[] imgBinaryData = new byte[imgLen];

                int n = imgStream.Read(imgBinaryData, 0, imgLen);

                int RowsAffected = SaveToDB(imgName, imgBinaryData, imgContentType);
                if (RowsAffected > 0)
                {
                    Response.Write("<br />Image Saved<br />");
                }
                else
                {
                    Response.Write("<br />An error has occurred, please upload again<br />");
                }
            }
        }






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert image in sql 2000 mangala SQL Server 2000 18 December 17th, 2007 06:11 AM
MS ACCESS 2003 FRONTEND AND MS SQL SERVER 2005 DB mohankumar0709 SQL Server 2005 3 March 23rd, 2007 12:48 AM
Need help on MS SQL Server 005 Express INSERT cmd VictorVictor ASP.NET 2.0 Professional 7 November 23rd, 2006 02:12 AM
how to insert the path of image in sql server hoailing22 ASP.NET 1.0 and 1.1 Basics 0 February 28th, 2006 03:37 AM
How to insert path of the image in sql server hoailing22 SQL Server 2000 0 February 28th, 2006 03:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.