我在更新数据方面遇到了问题。
样品:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
string id = row.Cells[1].Text;
Response.Redirect("edit.aspx?id="+id);
}
此代码转换到具有update cmd的另一个页面后。
protected void Page_Load(object sender, EventArgs e)
{
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView drv in dv)
{
IDLBL.Text = drv["ID"].ToString();
Name.Text = drv["Name"].ToString();
SName.Text = drv["SecondName"].ToString();
Ocenka.Text = drv["Graduate"].ToString();
Klass.Text = drv["Class"].ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ARM_TSPConnectionString"].ConnectionString);
con.Open();
string upd = "UPDATE Info SET Name=@Name, SecondName=@SecondName, Graduate=@Graduate, Class=@Class WHERE ID=@ID";
SqlCommand cmd = new SqlCommand(upd, con);
cmd.Parameters.AddWithValue("@ID", IDLBL.Text);
cmd.Parameters.AddWithValue("@SecondName", SName.Text);
cmd.Parameters.AddWithValue("@Graduate", Ocenka.SelectedValue);
cmd.Parameters.AddWithValue("@Class", Klass.SelectedValue);
cmd.Parameters.AddWithValue("@Name", Name.Text);
cmd.ExecuteNonQuery();
Response.Redirect("main.aspx");
}
我点击了按钮,然后被重定向到主页面。但没有别的,更新不起作用。 :( 我在哪里遇到问题?
答案 0 :(得分:0)
您的更新sql查询看起来很好:
con.Open();
com.ExecuteNonQuery();
con.Close();
尝试使用此函数并调试它以查看是否抛出了sql异常。
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ARM_TSPConnectionString"].ConnectionString);
string upd = "UPDATE Info SET Name=@Name, SecondName=@SecondName, Graduate=@Graduate, Class=@Class WHERE ID=@ID";
SqlCommand cmd = new SqlCommand(upd, con);
cmd.Parameters.AddWithValue("@ID", IDLBL.Text);
cmd.Parameters.AddWithValue("@SecondName", SName.Text);
cmd.Parameters.AddWithValue("@Graduate", Ocenka.SelectedValue);
cmd.Parameters.AddWithValue("@Class", Klass.SelectedValue);
cmd.Parameters.AddWithValue("@Name", Name.Text);
con.Open();
com.ExecuteNonQuery();
con.Close();
Response.Redirect("main.aspx");
}
catch (SqlException e)
{
}
}
更新:想想我已经意识到为什么它不适合你...你有一个名为class的列...但是sql server中的类是一个保留关键字...所以你必须把方括号放在它周围......就像这样
编辑逃跑(汉斯在下面的评论中指出错误)
string upd = "UPDATE Info SET Name=@Name, SecondName=@SecondName, Graduate=@Graduate, [Class]=@Class WHERE ID=@ID";
答案 1 :(得分:0)
我真的不知道问题出在哪里,但是你肯定需要使用连接对象的IDisposable功能重构你的更新语句,它应该是这样的:
using (SqlConnection connection = new SqlConnection(
ConfigurationManager.ConnectionStrings["ARM_TSPConnectionString"].ConnectionString))
{
string upd = "UPDATE Info SET Name=@Name, SecondName=@SecondName, Graduate=@Graduate, Class=@Class WHERE ID=@ID";
SqlCommand cmd = new SqlCommand(upd, connection);
cmd.Parameters.AddWithValue("@ID", IDLBL.Text);
cmd.Parameters.AddWithValue("@SecondName", SName.Text);
cmd.Parameters.AddWithValue("@Graduate", Ocenka.SelectedValue);
cmd.Parameters.AddWithValue("@Class", Klass.SelectedValue);
cmd.Parameters.AddWithValue("@Name", Name.Text);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
答案 2 :(得分:0)
发现问题。如果(!isPostBack)
,只需添加Page_Loadprotected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView drv in dv)
{
IDLBL.Text = drv["ID"].ToString();
Name.Text = drv["Name"].ToString();
SName.Text = drv["SecondName"].ToString();
Ocenka.Text = drv["Graduate"].ToString();
Klass.Text = drv["Class"].ToString();
}
}
现在,一切顺利。