我试图从我的查询中获取一个值。我得到一个sintax错误。 它包含图片名称mypic.jpg。然后我想在我的页面上显示它。
这就是我正在做的如何正确地做到这一点。
display.aspx
<asp:Image ID="Img1" ImageUrl="pathToPicture" runat="server" />
display.aspx.cs
Picture DLtestPicture = new Picture();
DataTable DTTestPicture = DLtestPicture.GetRandomPicture();
String pathToPicture = DTTestPicture.Rows(0).Item("PicLoc").ToString();
答案 0 :(得分:2)
您需要使用索引器:
String pathToPicture = DTTestPicture.Rows[0]["PicLoc"].ToString();
或者 - 最好是IMO - 更改GetRandomPicture
以返回强类型值,而不是DataTable
。它可以至少只显式返回一个DataRow
,而不是返回一个表。
答案 1 :(得分:1)
if (DTTestPicture.Rows.Count >= 0)
{
string pathToPicture= Convert.ToString(DTTestPicture.Rows[0]["PicLoc"]);
}
答案 2 :(得分:1)
检查这个
DTTestPicture.Rows(0).Item("PicLoc")
它不是C#语法。在基于[]
/索引的对象()
的位置使用containers
。
更正语法:
DTTestPicture.Rows[0].Item["PicLoc"]
在toString()
使用Convert.ToString()
的地方,以避免空值异常。
Convert.ToString(DTTestPicture.Rows[0].Item["PicLoc"]);