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 August 24th, 2004, 01:50 PM
Registered User
Join Date: Aug 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Keep inserting the same image to DB

Greetings all,

I have made a form on my C# application for adding new images (and info about the image) into a MSSQL 2000 database. I have two buttons for adding the image; one for adding it and closing the form and one for adding it keeping the form open to add another. Both call on the same functions to add the image but when I try to add another, it always adds the same image as it did the first time.

My question is, when you declare a byte[] inside a function, does the byte[] not get reset the next time the function is called? If not, is there a way that I can destroy the byte array at the end of the function to make sure the same data doesn't get added the next time the function is called. My byte arrays inside the function are declared like this:

    byte[] rawstandard = GetPhoto(standpath);
    byte[] rawthumb = GetPhoto(thumbpath);

rawstandard and rawthumb are the standard size and thumbnail size raw byte arrays. standpath and thumbpath are the absolute paths of the image files (They get created at the beginning of the function and deleted at the end of the function). The GetPhoto function looks like this:

public static byte[] GetPhoto(string filePath)
    FileStream fs = new FileStream(filePath, FileMode.Open,
    BinaryReader br = new BinaryReader(fs);

    byte[] photo = br.ReadBytes((int)fs.Length);


    return photo;

Can anyone see a reason why it would be using the same byte data the second time as it would the first, even though the files in standpath and thumbpath have changed?

Old August 24th, 2004, 02:16 PM
Registered User
Join Date: Aug 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

Oops. Problem solved. The problem wasn't the byte array at all. The database object (from my own class) was declared in the form constructor rather than inside the function, meaning the paramaters in the command object were not refreshing themselves.

I knew all I had to do was post it here and I would figure it out .


Old August 31st, 2004, 04:41 PM
Authorized User
Join Date: Jul 2004
Posts: 69
Thanks: 0
Thanked 1 Time in 1 Post

The scope of the variable defines when it will be reinitialized, btw.

Get A Computer Job!

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting data into DB roserose PHP Databases 1 April 24th, 2006 07:54 AM
Inserting records into DB using DataAdapter favor ADO.NET 4 June 16th, 2005 02:29 AM
How do I check a db before inserting a record? Lucy SQL Server ASP 3 April 25th, 2005 10:47 PM
Inserting into access db using VWD. Peki ASP.NET 2.0 Basics 1 April 11th, 2005 09:12 PM
Inserting a value containing ' into a db jfrizelle Classic ASP Databases 2 November 10th, 2004 08:55 AM

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