Jimmysnn
Jimmysnn

Reputation: 593

download a photo from database c#

I try to download a photo from the database. In database, the field type is Image. The ID is type UniqueIdentifier. My code

public void ProcessRequest (HttpContext context) {
    ConnectTodatabase conection = new ConnectTodatabase();
    conection.makeConnection();

    // Create SQL Command 
    SqlCommand cmd = new SqlCommand("Select ID,photo from profile where ID=@ID", conection.Connection);
    SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.UniqueIdentifier);
    ImageID.Value = context.Request.QueryString["ID"];
    cmd.Parameters.Add(ImageID);

    SqlDataReader dReader = cmd.ExecuteReader();
    dReader.Read();
    context.Response.BinaryWrite((byte[])dReader["photo"]);
    dReader.Close();


    context.Response.ContentType = "text/plain";
    context.Response.Write("test 123456");
}

The Exception is InvalidCastException. "Failed to convert parameter value from a String to a Guid." How to pass ImageID to correct type? Thanks!!!

call handler

foreach (DataRow theRow in thisDataSet.Tables["Profile"].Rows)
        {
            resultCounter++; 
            double x, y;
            x = Convert.ToDouble(theRow["lat"]);
            y = Convert.ToDouble(theRow["lng"]);
            string id = Convert.ToString(theRow["ID"]);
            GLatLng latlng = new GLatLng(x, y);//sintetagmenes shmeiou
            //dimiourgia ton 2 ipomenou gia kathe shmeio
            GInfoWindowTabs iwTabs = new GInfoWindowTabs();
            iwTabs.point = latlng;

            System.Collections.Generic.List<GInfoWindowTab> tabs = new System.Collections.Generic.List<GInfoWindowTab>();

            tabs.Add(new GInfoWindowTab("Profile Info:", "<table> <tr> <td><b>Name: </b> </td><td>" + theRow["fname"] + "</td></tr><tr><td><b>Lastname: </b></td><td>" + theRow["lname"] + "</td></tr><tr><td><b>Affiliation: </b></td><td>" + theRow["affiliation"] + "</td></tr><tr><td><b>Address: </b></td><td>" + theRow["address"] + "</td></tr><tr><td><b>Country: </b></td><td>" + theRow["country"] + "</td></tr><tr><td><b>Email: </b></td><td>" + theRow["email"] + "</td></tr><tr><td><b>Role: </b></td><td>" + theRow["role"]));


            tabs.Add(new GInfoWindowTab("Profile Photo:", "<img src=Handler.ashx?ID=" + theRow["ID"] + "border=1>"));

Upvotes: 1

Views: 877

Answers (2)

Jeremy
Jeremy

Reputation: 9030

Try this and see if it works:

SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.UniqueIdentifier);
ImageID.Value = System.Data.SqlTypes.SqlGuid.Parse(context.Request.QueryString["ID"]);

The key here is in converting the string to a GUID.

EDIT

I noticed that the edit you made includes your call handler. Two glaring issues I see with your call handler is that you created an id variable to hold your converted GUID, but you aren't acually using it. Also, you aren't properly delimiting your border query parameter using the ampersand: &

Change this:

tabs.Add(new GInfoWindowTab("Profile Photo:", "<img src=Handler.ashx?ID=" + theRow["ID"] + "border=1>"));

to this:

tabs.Add(new GInfoWindowTab("Profile Photo:", "<img src=Handler.ashx?ID=" + id + "&border=1>"));

So, in essence, you are sending this as your ID: 5a6b4047-e2dc-40d8-9c58-8609278154f4border=1 which isn't a valid GUID.

Just as a tip, I would have done it like this which would have likely made it easier to spot your Type-O:

tabs.Add(new GInfoWindowTab("Profile Photo:", string.Format("<img src=Handler.ashx?ID={0}&border=1>", id)));

Upvotes: 4

TimWagaman
TimWagaman

Reputation: 1008

Guid ImageGUID = new Guid(context.Request.QueryString["ID"]);

ImageID.Value = ImageGUID;

Upvotes: 0

Related Questions