【发布时间】:2014-08-13 13:25:38
【问题描述】:
我正在编写代码,从数据库中检索一个值并将其存储在一个字符串变量中,然后再显示它。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
Int32 num = Convert.ToInt32(e.CommandArgument);
String s = Request.QueryString["field1"];
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string getAddress = "select Address from Patrons where Email='" + s + "'";
SqlCommand com = new SqlCommand(getAddress, conn);
String addr = Convert.ToString(com.ExecuteScalar());
Response.Write(GridView1.Rows[num].Cells[1].Text + " will be mailed to " + addr);
conn.Close();
}
}
当我运行它时,显示 GridView1.Rows[num].Cells[1].Text + "will be mailed to "。但是,没有显示具有 Email = s 的人的地址。
【问题讨论】:
-
不是您问题的答案,但请使用参数为 sql 查询提供值。 (见bobby-tables.com)
-
如果你调试程序,在ExecuteScalar之后break,你能看到addr变量中的值吗?
-
您确定
s的值是该 Patrons 表中的电子邮件地址并且它有一个地址吗? -
您是否运行过调试并确保 addr 实际上得到了一个值?