asp.net - download a photo from database c# -


i try download photo database. in database, field type image. id type uniqueidentifier. code

public void processrequest (httpcontext context) {     connecttodatabase conection = new connecttodatabase();     conection.makeconnection();      // create sql command      sqlcommand cmd = new sqlcommand("select id,photo profile 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 invalidcastexception. "failed convert parameter value string guid." how pass imageid 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>")); 

try , see if works:

sqlparameter imageid = new sqlparameter("@id", system.data.sqldbtype.uniqueidentifier); imageid.value = system.data.sqltypes.sqlguid.parse(context.request.querystring["id"]); 

the key here in converting string guid.

edit

i noticed edit made includes call handler. 2 glaring issues see call handler created id variable hold converted guid, aren't acually using it. also, aren't delimiting border query parameter using 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, sending id: 5a6b4047-e2dc-40d8-9c58-8609278154f4border=1 isn't valid guid.

just tip, have done have made easier spot type-o:

tabs.add(new ginfowindowtab("profile photo:", string.format("<img src=handler.ashx?id={0}&border=1>", id))); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -