Uploading An Image from a Windows App to A Database via webservices

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Eric Paul (ericallenpaul_at_hotmail.com)
Date: 03/08/04


Date: 8 Mar 2004 10:50:14 -0800

I searched for the answer to this question when I started this new
project and I couldn't find a specific example of uploading an image
into SQL via a web service from a Windows Application. I found a ton
of other examples which got me close, but weren't exactly what I was
looking for.
As is my policy, when I look for an answer and don't find one I try to
publish mine (beware bad line breaks).

In your web service add the following:

[WebMethod]
public int SaveImage(Byte[] imgdata)
{
        //use the web.config to store the connection string
        SqlConnection connection = new SqlConnection("YourConnectionString");

        //build the SQL command
        //Put your insert statement here--the "Image" column needs to
        //be of type "image"
        SqlCommand command = new SqlCommand( "INSERT INTO ContactsImages
(ImageData) VALUES (@img_data )", connection );

        //build the parameters
        command.Parameters.Add(new SqlParameter("@img_data", imgdata));
                        
        //open the datbase connection
        try
        {
                //if the DB connection is not already open, open it
                if(connection.State != ConnectionState.Open)
                {
                        connection.Open();
                }
        }
        catch
        {
                //can't open the conenction to the database--throw an error
                Exception oException= new Exception("An error occured while opening
connection to the database.");
                throw oException;
        }

        //perform the sql command and store the results
        int numRowsAffected = command.ExecuteNonQuery();

        //close the database connection
        connection.Close();
                        
        //answer with the number of rows - 1 if successful
        return numRowsAffected;

}

Then in your windows application add a "web refrence" to the web
service and use code like the following:

private void btnUpload_Click(object sender, System.EventArgs e)
{
        String strBLOBFilePath = textBox1.Text; //textbox with name and path
to image
        FileStream fsBLOBFile = new
FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read);
        Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
        fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
        fsBLOBFile.Close();
        MYWEBSERVICE.MYSERVICENAME IDS = new MYWEBSERVICE.MYSERVICENAME();
        int Output = IDS.SaveImage(bytBLOBData);
}

The key is to convert the image to a byte[] array before you send it.
Hope someone else finds this useful.



Relevant Pages

  • Re: ADFS Development Issues
    ... One thing to keep in mind is that if a website is protected by ADFS V1, ... site to be automatically authenticated by our windows application so ... like a web service proxy. ... generated on the server. ...
    (microsoft.public.windows.server.active_directory)
  • Re: ADFS Development Issues
    ... site to be automatically authenticated by our windows application so ... based on redirects and possibly uses forms-based authentication to collect ... web service proxies don't handle this type of thing ... the server based on how it needs to work. ...
    (microsoft.public.windows.server.active_directory)
  • Re: Dynamically Change URL of web service
    ... Microsoft MVP - Windows Client ... > client apps will use the exact same web service, ... What this does is put an entry in the config file, ... >>> How can I dynamically change this when the client app loads. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • RE: Web Service Security
    ... You could encrypt or hash your ... As far as web service authentication/authorisation is concerned you would be ... WSE does a pretty good job of abstracting away most of the ... a windows domain account). ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Web Service, Authentication, Security & Domains
    ... The easy way to do this is with Basic authentication and SSL. ... that does send the password across the wire (although it is ... Windows application will communicate to the Web Service via internet ...
    (microsoft.public.dotnet.security)