Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi..

I want to store a default image in binary format,into database table when no image file is uploaded from fileupload control in my web page.i don't want to insert null values in "image" field of image table I have searched on google but couldn't find solution.

code sample i have tried is
C#
if(imgfileupload.hasfile)
{
//here i have converted the image file in fileupload control into byte[]
}
else
{
//here i want code for converting default image into byte[]
i.e if(imgfileupload.hasfile=="false")......
}

please I want source code.please i want source code.i really need it......

Thanks in advance
Posted
Updated 6-Jan-11 5:21am
v4
Comments
Sandeep Mewara 6-Jan-11 14:42pm    
Asking for source code desperately! Why so? Please avoid it.
Sandeep Mewara 6-Jan-11 14:43pm    
I have no clue, whats the issue! If nothing is uploaded, pick a file from default location and follow the save route. What's an issue here?
fjdiewornncalwe 6-Jan-11 15:07pm    
I'll vote 1 because you are obviously fishing for a solution without putting in effort yourself. You added the code block to your question after SChristmas posted his answer below for the if block logic. Now you want the "convert to byte[]" logic. How about you try that and then post a question about that instead of fishing for someone to do your homework for you.

At the time of uploading you check the .HasFile property of FileUpload control (it will be true if file is selected), as following:
if (FileUpload1.HasFile)
            {
                // Upload Browsed File
            }
            else
            {
                // Use Default File
            }


Accept the answer if found useful :)
 
Share this answer
 
v2
Comments
fjdiewornncalwe 6-Jan-11 15:08pm    
+5 because the OP obviously has used this by posting a variation of your answer in his question after reading your answer.
If you have the image as bitmap, you can use this

C#
private byte[] GetByteArray(Bitmap bitmapImage)
{
    var byteArray = (byte[])TypeDescriptor.GetConverter(bitmapImage).ConvertTo(bitmapImage, typeof(byte[]));
    bitmapImage.Dispose();
    return byteArray;
}



And in fact, for each situation you don't have to be putting the same default image repeatedly in the database, so, I think you should just store it once and retrieve it for every case where there is no default image. With regards to storing null image in the respected column, I believe a better practice is to have a separate table with two columns where you put the entity primary key as foreign key and the second column will contain the image, this way, any item with no record in this table has no image of its own, sow we get the standard default image that is store in just one location.
 
Share this answer
 
Comments
fjdiewornncalwe 6-Jan-11 15:10pm    
Good answer, but I have one problem with it. Because the bitmapImage object is being sent into the method as a reference, I would not dispose of it here because you can never be sure that the calling code doesn't do something else with the object after calling this method. (Just something that popped out at me)
Manfred Rudolf Bihy 6-Jan-11 15:39pm    
@Marcus: Now I just can't wait until OP pastes this into his question and starts asking for the code to save it to a database. What'll be next? I guess reading the default image from a file... Never ending story!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900