【发布时间】:2018-10-12 06:58:14
【问题描述】:
我正在尝试在 gridview 中插入数据,但它没有显示任何错误,因为数据也在插入中。
我的代码:
protected void InsertData1(object sender, EventArgs e)
{
try
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlserver"].ConnectionString);
TextBox t1 = (TextBox)GridView1.FooterRow.FindControl("TextBox2");
TextBox t2 = (TextBox)GridView1.FooterRow.FindControl("TextBox4");
TextBox t3 = (TextBox)GridView1.FooterRow.FindControl("TextBox6");
TextBox t4 = (TextBox)GridView1.FooterRow.FindControl("TextBox8");
TextBox t5 = (TextBox)GridView1.FooterRow.FindControl("TextBox10");
TextBox t6 = (TextBox)GridView1.FooterRow.FindControl("TextBox12");
cmd = new SqlCommand("insert into tblHelpDesk (Name,Purpose,ContactNo,AlternativeNo,Email,Address) values(@Name,@Purpose,@ContactNo,@AlternativeNo,@Email,@Address)", con);
con.Open();
cmd.Parameters.AddWithValue("@Name", t1.Text);
cmd.Parameters.AddWithValue("@Purpose", t2.Text);
cmd.Parameters.AddWithValue("@ContactNo", t3.Text);
cmd.Parameters.AddWithValue("@AlternativeNo", t4.Text);
cmd.Parameters.AddWithValue("@Email", t5.Text);
cmd.Parameters.AddWithValue("@Address", t6.Text);
cmd.ExecuteNonQuery();
Page.Response.Redirect(Page.Request.Url.ToString(), true);
}
}
catch (Exception ex)
{ }
}
【问题讨论】:
-
输出 cmd 查询,以便您尝试直接运行以帮助调试问题
-
将
throw ex放入catch (Exception ex),看看当前查询状态返回了什么错误。 -
Response.Redirect with true 参数结束当前页面的执行。您将它放在循环中,因此最多将添加一条记录
-
即使每次都重定向,为什么还要进行循环。
-
让我们忽略我们可以用这段代码删除整个表的事实......