如何从数据库中检索数据值并将其存储在字符串中?

时间:2014-08-13 13:25:39

标签: c# sql asp.net webforms

我正在编写代码,从数据库中检索一个值并将其存储在字符串变量中,然后再显示它。

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 +"将被邮寄到"被展示。但是,没有显示具有Email = s的人的地址。

1 个答案:

答案 0 :(得分:-1)

  

首先分配查询字符串,如下所示

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

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