更新 SQL 查询不起作用 ASP.NET Web 应用程序

问题描述

这是我的 .aspx 页面代码, 当我单击更新时,它运行的页面不会显示任何错误,并且我的博客表中的行未更新。 异常不显示任何内容,我的表中有一行带有 id。

Page_Load() 中的选择命令完美运行。

using System;
using System.Collections.Generic;
using System.Data.sqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Blog
{
    public partial class update : System.Web.UI.Page
    {
        sqlConnection connection = new sqlConnection(##################);

        protected void Page_Load(object sender,EventArgs e)
        {
            string id = Request.QueryString["id"];
            string sql = "select * from blog where Id=@id";
            sqlCommand sqlCommand = new sqlCommand(sql,connection);
            sqlCommand.Parameters.AddWithValue("@id",id);
            connection.open();
            sqlDataReader dataReader = sqlCommand.ExecuteReader();
            while (dataReader.Read())
            {
                DropDownListCategory.Selectedindex = DropDownListCategory.Items.IndexOf(DropDownListCategory.Items.FindByText(dataReader["category"].ToString().Trim()));
                TextBoxTitle.Text = dataReader["title"].ToString();
                TextBoxDesc.Text = dataReader["description"].ToString();
            }
            connection.Close();
        }
        protected void Timer1_Tick(object sender,EventArgs e)
        {
            LabelDate.Text = DateTime.Now.ToString();
        }

        protected void Update_Click(object sender,EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(Request.QueryString["id"].Trim());
                string sql2 = "UPDATE blog SET title = @title,category = @category,description = @description,posteddate = @posteddate WHERE Id=@id";
                connection.open();

                sqlCommand sqlCommand2 = new sqlCommand(sql2,connection);
                sqlCommand2.Parameters.AddWithValue("@id",id);
                sqlCommand2.Parameters.AddWithValue("@title",TextBoxTitle.Text);
                sqlCommand2.Parameters.AddWithValue("@category",DropDownListCategory.SelectedItem.Text.ToString());
                sqlCommand2.Parameters.AddWithValue("@description",TextBoxDesc.Text);
                sqlCommand2.Parameters.AddWithValue("@posteddate",DateTime.Now.ToString());
                sqlCommand2.ExecuteNonQuery();
            }
            catch(Exception ex)
            {
                Label1.Text = ex.Message;
            }
            connection.Close();
            //Response.Redirect("Default.aspx");
        }

        
    }
}

解决方法

添加了 if(!IsPostBack){} 现在可以正常工作了,谢谢

  protected void Page_Load(object sender,EventArgs e)
            {
                if (!IsPostBack)
                {
                    string id = Request.QueryString["id"];
                    string sql = "select * from blog where Id=@id";
                    SqlCommand sqlCommand = new SqlCommand(sql,connection);
                    sqlCommand.Parameters.AddWithValue("@id",id);
                    connection.Open();
                    SqlDataReader dataReader = sqlCommand.ExecuteReader();
                    while (dataReader.Read())
                    {
                        DropDownListCategory.SelectedIndex = DropDownListCategory.Items.IndexOf(DropDownListCategory.Items.FindByText(dataReader["category"].ToString().Trim()));
                        TextBoxTitle.Text = dataReader["title"].ToString();
                        TextBoxDesc.Text = dataReader["description"].ToString();
                    }
                    connection.Close();
                }
            }