Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to upload an image in the database via WCF. I'm bulding a WP 8 app that will upload image in the database and then show it on the user's profile page. Didn't find anything related to WP except for this tutorial . I'm trying to follow it, but get errors. Here is my service implementation:
C#
public bool Upload(Stuff picture)
        {
            
            FileStream fileStream = null;
            BinaryWriter writer = null;
            string filePath;

            try
            {
                filePath = HttpContext.Current.Server.MapPath(".") +
                           ConfigurationManager.AppSettings["PictureUploadDirectory"] +
                           picture.stuffName; 

                if (picture.stuffName != string.Empty)
                {
                    fileStream = File.Open(filePath, FileMode.Create);
                    writer = new BinaryWriter(fileStream); 
                    writer.Write(picture.stuffPhoto); //error 1,2
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
                if (writer != null)
                    writer.Close();
            }
        }

Errors:
1) The best overloaded method match for 'System.IO.BinaryWriter.Write(string)' has some invalid arguments.
2) Argument 1: cannot convert from 'System.Data.Linq.Binary' to 'string'.
3) DBML1005: Mapping between DbType 'VarBinary(MAX)' and Type 'System.Data.Linq.Binary' in Column 'stuffPhoto' of Type 'Stuff' is not supported.

There is no code for error 3. I defined stuffPhoto (member of Stuff class) as VarBinary(MAX) type and I think it is not supported by the LINQ to SQL Classes. But I read that I should define this type in the database to convert my images in the byte array. Those tutorials were not using LINQ to SQL Classes, so I don't know what type is right in my case.
Posted
Updated 16-Jun-14 2:09am
v4
Comments
[no name] 15-Jun-14 6:46am    
"Guidance"? Could not find what "in" the Internet? Must be thousands of examples out there for getting an image from a database and the Image class has a StreamSource property that you can use. So what is the actual problem that you are having with your code?
Purple Cucumber 15-Jun-14 7:05am    
Tutorials about image control show how to get an image from the local folder using url. But I want to know how can I insert the image into the database using WCF service.
[no name] 15-Jun-14 7:15am    
You don't. You use a WFC service to connect to your database and write SQL to insert the image into the database. And this has nothing at all to do with your question.
Purple Cucumber 15-Jun-14 8:19am    
And how do I invoke a dialog where I should choose picture from my PC to upload in the database on my WP page?
CHill60 15-Jun-14 8:28am    
There are several similar questions with solutions here on CodeProject ... have a look at this list http://www.codeproject.com/search.aspx?q=image+database+WCF[^]

1 solution

First things first, ensure all the right dlls are referenced.

For e.g. the class Stuff probably requires a reference.
That is why you get the The type or namespace name 'stPic' could not be found (are you missing a using directive or an assembly reference?) error.
 
Share this answer
 
Comments
Purple Cucumber 16-Jun-14 8:12am    
I edited my question, added using statements in the code and a few error are gone. But I don't know what to do with those three.

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