更新命令在gridview中不起作用

时间:2012-04-28 19:30:54

标签: asp.net

我的更新命令代码:

public void update(name,email,phone)
{
    using {sqlconnection con=new sqlconnection("some connection source"))
    {
        con.open();
        string str = "UPDATE register SET name = @name,email = @email WHERE phone = "@phone";

        using(sqlcommand cmd=new sqlcommand(str,con))
        {
            cmd.commandtype=commandtype.text;
            cmd.parameters.addwithvalue("@name",name);
            cmd.parameters.addwithvalue("@email",email);
            cmd.parameters.addwithvalue("@phone",phone);
            cmd.executenonquery();
        }
    }
}

我的行更新事件是这样的:

string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string email = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
int phone = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[3].Text);

update(name, email, phone);
GridView1.EditIndex = -1;
bindgridview();

在我的表中,手机是主键,当我按下更新按钮时,它不会触发,不会发生任何变化。任何人都可以看到我的代码并告诉我我做错了什么

1 个答案:

答案 0 :(得分:0)

更新语句中缺少简单Quotation(“),使用后不必要{,并确保您的conn也已关闭

相关问题