【发布时间】:2012-12-17 18:05:52
【问题描述】:
我在网格视图中有这张表。
ID Question_No Question Survey_ID
-----------------------------------------------------------
1 1 Whats you name? 44
2 2 How Old Are you? 44
3 3 Whats your favorite hobby 44
4 4 What did you study? 44
我想在页面上添加一个删除按钮,如下所示:当我删除其中一条记录时,只要调查 ID 为 44,我想自动更新所有问题的 question_no。例如,如果我删除第二个问题,会变成这样。
ID Question_No Question Survey_ID
-----------------------------------------------------------
1 1 Whats you name? 44
3 2 Whats your favorite hobby 44
4 3 What did you study? 44
我该怎么做?我想它一定是一个循环,但我什至不知道如何处理它。
编辑:这是我的删除按钮代码
protected void RemoveQuestionButton_Click(object sender, EventArgs e)
{
try
{
DataRowView r;
r = ((DataRowView)QuestionsGridView.GetRow(QuestionsGridView.FocusedRowIndex));
Session["Question_ID"] = r[0];
if (Session["Question_ID"] != null)
{
SqlConnection connection = DatabaseConnection.GetSurveySystemConnection();
string delStatement1 = "DELETE FROM Questions WHERE ID =" + Session["Question_ID"];
string delStatement2 = "DELETE FROM Question_Options where Question_ID=" + Session["Question_ID"];
SqlCommand cmd = new SqlCommand(delStatement1, connection);
SqlCommand cmd2 = new SqlCommand(delStatement2, connection);
cmd.CommandType = CommandType.Text;
cmd2.CommandType = CommandType.Text;
try
{
cmd2.ExecuteNonQuery();
cmd.ExecuteNonQuery();
ConfirmLbl.ForeColor = System.Drawing.ColorTranslator.FromHtml("Green");
ConfirmLbl.Text = "Question & Options Deleted Successfully!";
QuestionsGridView.DataBind();
}
catch (Exception)
{
ConfirmLbl.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
ConfirmLbl.Text = "This Question Has Options Linked to it...";
}
finally
{
connection.Close();
}
}
}
catch (Exception)
{
ConfirmLbl.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
ConfirmLbl.Text = "You need to select a Question to edit...";
}
}
【问题讨论】:
-
如果他甚至不知道如何接近它,似乎什么都没有。
-
您是否有任何尝试自己编写的 C# 代码..?为您提供快速答案并不能真正帮助您学习 C#,请花时间去谷歌搜索,或者先尝试自己编写代码..
-
您是从数据库还是从其他来源提取这些数据?如果是这样,当您删除一个问题时,您是从源中删除该问题吗?
-
是的,我从连接到 sqlserver 的 sqldatasource 中提取它,是的,我从源中删除它,我现在正在编辑我的问题,为您提供我所拥有的。
标签: c# asp.net sql sql-server-2012