C# 从数据库c下载一张照片#

C# 从数据库c下载一张照片#,c#,asp.net,sql,C#,Asp.net,Sql,我试着从数据库下载一张照片。在数据库中,字段类型为Image。 ID的类型为UniqueIdentifier。 我的代码 异常为InvalidCastException。“无法将参数值从字符串转换为Guid。” 如何将ImageID传递给正确的类型? 谢谢 呼叫处理程序 foreach (DataRow theRow in thisDataSet.Tables["Profile"].Rows) { resultCounter++;

我试着从数据库下载一张照片。在数据库中,字段类型为Image。 ID的类型为UniqueIdentifier。 我的代码

异常为InvalidCastException。“无法将参数值从字符串转换为Guid。” 如何将ImageID传递给正确的类型? 谢谢

呼叫处理程序

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>"));
foreach(DataRow theRow在此数据集中。表[“Profile”]。行)
{
结果计数器++;
双x,y;
x=转换为双字节(row[“lat”]);
y=换算为双倍(以下简称“液化天然气”);
字符串id=Convert.ToString(row[“id”]);
GLatLng latlng=新GLatLng(x,y);//sintetagmenes shmeiou
//伊波梅努2号酒店
GInfoWindowTabs iwtab=新的GInfoWindowTabs();
IWTAB.point=车床;
System.Collections.Generic.List tabs=新建System.Collections.Generic.List();
添加(新的GInfoWindowTab(“个人资料信息:”,“姓名:”+theRow[“fname”]+“姓氏:”+theRow[“lname”]+“附属关系:”+theRow[“附属关系”]+“地址:”+theRow[“地址”+“国家:”+“电子邮件:”+theRow[“电子邮件”+“角色:”));
添加(新的GInfoWindowTab(“个人资料照片:,”);

试试这个,看看它是否有效:

SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.UniqueIdentifier);
ImageID.Value = System.Data.SqlTypes.SqlGuid.Parse(context.Request.QueryString["ID"]);
这里的关键是将字符串转换为GUID

编辑

我注意到您所做的编辑包括您的调用处理程序。我看到您的调用处理程序有两个突出的问题,即您创建了一个id变量来保存转换后的GUID,但您没有实际使用它。此外,您没有使用符号和正确地分隔边界查询参数:&

更改此项:

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

试试这个,看看它是否有效:

SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.UniqueIdentifier);
ImageID.Value = System.Data.SqlTypes.SqlGuid.Parse(context.Request.QueryString["ID"]);
这里的关键是将字符串转换为GUID

编辑

我注意到您所做的编辑包括您的调用处理程序。我看到您的调用处理程序有两个突出的问题,即您创建了一个id变量来保存转换后的GUID,但您没有实际使用它。此外,您没有使用符号和正确地分隔边界查询参数:&

更改此项:

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

我有这个错误:Guid应该包含32位数字和4个破折号(xxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx-xxxxxxxxxxxx)。从ASP.NET配置创建新用户时从visual studio自动创建的Id显示一个示例Id。需要确保它实际上是GUID。5a6b4047-e2dc-40d8-9c58-8609278154f4Yup,这是有效的。现在,您是否验证了
context.Request.QueryString[“Id”]
是否包含有效的ID?请添加一个断点并对其进行分析以确定。我有一个错误:Guid应包含32位数字和4个破折号(xxxxxxxx-xxxx-xxxx-xxxx-XXXXXXXXXXXXXXXXXX)。从ASP.NET配置创建新用户时从visual studio自动创建的Id显示一个示例Id。需要确保它实际上是GUID。5a6b4047-e2dc-40d8-9c58-8609278154f4Yup,这是有效的。现在,您是否验证了
context.Request.QueryString[“Id”]
包含有效的ID?请添加一个断点并对其进行分析以确保正确。我有相同的错误:Guid应包含32位数字和4个破折号(xxxxxxxx-xxxx-xxxx-xxxx-XXXXXXXXXXXXXXXX)。从ASP.NET配置创建新用户时,从visual studio自动创建的ID Guid对我来说没有问题。您确定它没有{}是用引号括起来还是用引号括起来?我有同样的错误:Guid应该包含32位数字和4个破折号(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。从ASP.NET配置创建新用户时,从visual studio自动创建的Id对我来说,该Guid运行正常。您确定它周围没有{}或引号吗?
tabs.Add(new GInfoWindowTab("Profile Photo:", string.Format("<img src=Handler.ashx?ID={0}&border=1>", id)));
Guid ImageGUID = new Guid(context.Request.QueryString["ID"]);

ImageID.Value = ImageGUID;