使用asp.net中的文本框更新sql server中的记录

时间:2014-05-14 10:19:22

标签: asp.net sql-server

我在更新数据库记录中的记录时遇到了困难。我通过在Page_Load

中包含此代码,将值分配给sqlserver中的文本框
protected void Page_Load(object sender, EventArgs e)
{
    string pn = (string)Session["prodname"];

SqlConnection camConnection = new SqlConnection();
private string s;
    s = WebConfigurationManager.ConnectionStrings["polaroidConnectionString"].ConnectionString;
    camConnection = new SqlConnection(s);

    SqlCommand select = new SqlCommand("Select * From productInfo Where ProductName= '" + pn + "'", camConnection);
    SqlDataReader camread;
    camConnection.Open();
    camread = select.ExecuteReader();
    while (camread.Read())
    {
        cat2.Text = camread[0].ToString();
        id2.Text = camread[1].ToString();
        pn2.Text = camread[2].ToString();
        desc2.Text = camread[3].ToString();
        price2.Text = camread[5].ToString();
    }

    camread.Close();
    camConnection.Close();

}

当我运行程序时,记录显示在文本框中。然后,我在Button1_Click中插入代码,这样当我更改文本框的值时,更改将在数据库中进行。这是我的Button1_Click代码。

protected void Button1_Click(object sender, EventArgs e)
{
    camConnection = new SqlConnection();
    private string s;
    s =     WebConfigurationManager.ConnectionStrings["polaroidConnectionString"].ConnectionString;
    camConnection = new SqlConnection(s);

    camConnection.Open();

    SqlCommand cmd = new System.Data.SqlClient.SqlCommand("update productInfo set ProductName= '" + pn2.Text + "', Description='" + Convert.ToString(desc2.Text) + "' where ID='" + Convert.ToString(id2.Text) + "' ", camConnection);

    cmd.ExecuteNonQuery();
    camConnection.Close();
}

但似乎系统无法识别我对textbox值所做的更改。任何形式的帮助将不胜感激。谢谢

0 个答案:

没有答案