【问题标题】:how to retrieve a data value from database and store it in string?如何从数据库中检索数据值并将其存储在字符串中?
【发布时间】: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 实际上得到了一个值?

标签: c# sql asp.net webforms


【解决方案1】:

首先你分配如下查询字符串

Response.Redirect("Getvalue.aspx?field1="+txtEmilId.Text+" OR Database Retrive Value);

使用此代码从查询字符串中获取值 lblDispEmailId.Text = Request.QueryString["field1"];

【讨论】:

  • 这与上述问题无关
猜你喜欢
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多