Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL Server:更新而不是使用C更新数据库#_C#_Sql_Sql Server_Visual Studio_Sql Update - Fatal编程技术网

C# SQL Server:更新而不是使用C更新数据库#

C# SQL Server:更新而不是使用C更新数据库#,c#,sql,sql-server,visual-studio,sql-update,C#,Sql,Sql Server,Visual Studio,Sql Update,我有一个网页,上面有一个网页表单。表单预先填充了SQL Server数据库中的数据。用户可以进入并编辑任何字段,然后单击底部的“保存”按钮以更新数据库中的记录。出于某种原因,当我用数据预填充表单时,update语句不会更新数据库中的记录。如果我在pageload方法中删除了预填充表单的代码,update语句将正常工作。当我在page load方法中用数据预先填充表单时,update语句似乎正在用记录中已经包含的数据更新记录,而不是用户输入的新信息。我完全不知道如何解决这个问题,因为表单中必须预加

我有一个网页,上面有一个网页表单。表单预先填充了SQL Server数据库中的数据。用户可以进入并编辑任何字段,然后单击底部的“保存”按钮以更新数据库中的记录。出于某种原因,当我用数据预填充表单时,update语句不会更新数据库中的记录。如果我在pageload方法中删除了预填充表单的代码,update语句将正常工作。当我在page load方法中用数据预先填充表单时,update语句似乎正在用记录中已经包含的数据更新记录,而不是用户输入的新信息。我完全不知道如何解决这个问题,因为表单中必须预加载数据,以便用户可以分别编辑表单

有人能指出我需要做的错误或更正吗?我撞到了众所周知的砖墙:(

下面是页面加载方法和保存按钮单击事件处理程序

protected void Page_Load(object sender, EventArgs e)
{
    String projectID = Request.QueryString["jobID"];
    String reportID = Request.QueryString["reportID"];

    string selectStatement = "SELECT * FROM ahu_data WHERE unit_ID = " + reportID;
    string sqlConnectionString = "Removed for Security";

    using (SqlConnection connection1 = new SqlConnection(sqlConnectionString))
    {
        SqlCommand selectCommand = new SqlCommand(selectStatement, connection1);
        connection1.Open();

        using (SqlDataReader reader = selectCommand.ExecuteReader())
        {
            while (reader.Read())
            {
                UMTextBox.Text = reader["make"].ToString();
                UMOTextBox.Text = reader["model"].ToString();
                UTTextBox.Text = reader["type"].ToString();
                USITextBox.Text = reader["size"].ToString();
                USTextBox.Text = reader["serial"].ToString();
                UATextBox.Text = reader["arrangement"].ToString();
                UCTextBox.Text = reader["class"].ToString();
                UDTextBox.Text = reader["discharge"].ToString();
                UMSTextBox.Text = reader["make_sheave"].ToString();
                USDTextBox.Text = reader["sheave_diameter"].ToString();
                USBTextBox.Text = reader["sheave_bore"].ToString();
                UBNTextBox.Text = reader["belts"].ToString();
                UBSTextBox.Text = reader["belt_size"].ToString();
                UFNTextBox.Text = reader["filters"].ToString();
                UFSTextBox.Text = reader["filter_size"].ToString();
                TCFMDTextBox.Text = reader["unitTotalCFMDesign"].ToString();
                TCFMATextBox.Text = reader["unitTotalCFMActual"].ToString();
                RACFMDTextBox.Text = reader["unitReturnAirCFMDesign"].ToString();
                RACFMATextBox.Text = reader["unitReturnAirCFMActual"].ToString();
                OACFMDTextBox.Text = reader["unitOutsideAirCFMDesign"].ToString();
                OACFMATextBox.Text = reader["unitOutsideAirCFMActual"].ToString();
                EACFMDTextBox.Text = reader["unitExhaustAirCFMDesign"].ToString();
                EACFMATextBox.Text = reader["unitExhaustAirCFMActual"].ToString();
                FRPMDTextBox.Text = reader["unitFanRPMDesign"].ToString();
                FRPMATextBox.Text = reader["unitFanRPMActual"].ToString();
                MRPMDTextBox.Text = reader["unitMotorRPMDesign"].ToString();
                MRPMATextBox.Text = reader["unitMotorRPMActual"].ToString();
                MVDTextBox.Text = reader["unitMotorVoltsDesign"].ToString();
                MVATextBox.Text = reader["unitMotorVoltsActual"].ToString();
                MADTextBox.Text = reader["unitMotorAmpsDesign"].ToString();
                MAATextBox.Text = reader["unitMotorAmpsActual"].ToString();
                MMTextBox.Text = reader["motor_make"].ToString();
                MFTextBox.Text = reader["motor_frame"].ToString();
                MHPTextBox.Text = reader["motor_hp"].ToString();
                MRPMTextBox.Text = reader["motor_rpm"].ToString();
                MVTextBox.Text = reader["motor_volts"].ToString();
                MPHTextBox.Text = reader["motor_phasehz"].ToString();
                MFLATextBox.Text = reader["motor_fl_amps"].ToString();
                MSFTextBox.Text = reader["motor_sf"].ToString();
                MMSTextBox.Text = reader["motor_make_sheave"].ToString();
                MSDTextBox.Text = reader["motor_sheave_diameter"].ToString();
                MSBTextBox.Text = reader["motor_sheave_bore"].ToString();
                MODTextBox.Text = reader["motor_operating_diameter"].ToString();
                MSCDTextBox.Text = reader["motor_sheave_center_distance"].ToString();
                TSPDTextBox.Text = reader["motorTotalSPDesign"].ToString();
                TSPATextBox.Text = reader["motorTotalSPActual"].ToString();
                ESPDTextBox.Text = reader["motorEnteringSPDesign"].ToString();
                ESPATextBox.Text = reader["motorEnteringSPActual"].ToString();
                SSPDTextBox.Text = reader["motorSuctionSPDesign"].ToString();
                SSPATextBox.Text = reader["motorSuctionSPActual"].ToString();
                DSPDTextBox.Text = reader["motorDischargeSPDesign"].ToString();
                DSPATextBox.Text = reader["motorDischargeSPActual"].ToString();
                PCSPDTextBox.Text = reader["motorPreheatCoilSPDesign"].ToString();
                PCSPATextBox.Text = reader["motorPreheatCoilSPActual"].ToString();
                CCSPDTextBox.Text = reader["motorCoolingCoilSPDesign"].ToString();
                CCSPATextBox.Text = reader["motorCoolingCoilSPActual"].ToString();
                RCSPDTextBox.Text = reader["motorReheatCoilSPDesign"].ToString();
                RCSPATextBox.Text = reader["motorReheatCoilSPActual"].ToString();
                FSPDTextBox.Text = reader["motorFilterSPDesign"].ToString();
                FSPATextBox.Text = reader["motorFilterSPActual"].ToString();
                AFSPDTextBox.Text = reader["motorAfterFilterSPDesign"].ToString();
                AFSPATextBox.Text = reader["motorAfterFilterSPActual"].ToString();
                WSPDTextBox.Text = reader["motorWheelSPDesign"].ToString();
                WSPATextBox.Text = reader["motorWheelSPActual"].ToString();
                RemarksTextArea.Text = reader["remarks"].ToString();
            }
        }
        connection1.Close();
    }
}
下面是保存按钮点击处理程序,它更新数据库中的记录

protected void SaveReportButton_Click(object sender, EventArgs e)
{
    String projectID = Request.QueryString["jobID"];
    String reportID = Request.QueryString["reportID"];
    string unitMake = UMTextBox.Text;
    string unitModel = UMOTextBox.Text;
    string unitType = UTTextBox.Text;
    string unitSize = USITextBox.Text;
    string unitSerial = USTextBox.Text;
    string unitArrangement = UATextBox.Text;
    string unitClass = UCTextBox.Text;
    string unitDischarge = UDTextBox.Text;
    string unitMS = UMSTextBox.Text;
    string unitSD = USDTextBox.Text;
    string unitSB = USBTextBox.Text;
    string unitBeltNumber = UBNTextBox.Text;
    string unitBeltSize = UBSTextBox.Text;
    string unitFilterNumber = UFNTextBox.Text;
    string unitFilterSize = UFSTextBox.Text;
    string unitTotalCFMDesign = TCFMDTextBox.Text;
    string unitTotalCFMActual = TCFMATextBox.Text;
    string unitReturnAirCFMDesign = RACFMDTextBox.Text;
    string unitReturnAirCFMActual = RACFMATextBox.Text;
    string unitOutsideAirCFMDesign = OACFMDTextBox.Text;
    string unitOutsideAirCFMActual = OACFMATextBox.Text;
    string unitExhaustAirCFMDesign = EACFMDTextBox.Text;
    string unitExhaustAirCFMActual = EACFMATextBox.Text;
    string unitFanRPMDesign = FRPMDTextBox.Text;
    string unitFanRPMActual = FRPMATextBox.Text;
    string unitMotorRPMDesign = MRPMDTextBox.Text;
    string unitMotorRPMActual = MRPMATextBox.Text;
    string unitMotorVoltsDesign = MVDTextBox.Text;
    string unitMotorVoltsActual = MVATextBox.Text;
    string unitMotorAmpsDesign = MADTextBox.Text;
    string unitMotorAmpsActual = MAATextBox.Text;
    string motorMake = MMTextBox.Text;
    string motorFrame = MFTextBox.Text;
    string motorHP = MHPTextBox.Text;
    string motorRPM = MRPMTextBox.Text;
    string motorVolts = MVTextBox.Text;
    string motorPhaseHz = MPHTextBox.Text;
    string motorFullLoadAmps = MFLATextBox.Text;
    string motorSF = MSFTextBox.Text;
    string motorMakeSheave = MMSTextBox.Text;
    string motorSheaveDiameter = MSDTextBox.Text;
    string motorSheaveBore = MSBTextBox.Text;
    string motorOperatingDiameter = MODTextBox.Text;
    string motorSheaveCDistance = MSCDTextBox.Text;
    string motorTotalSPDesign = TSPDTextBox.Text;
    string motorTotalSPActual = TSPATextBox.Text;
    string motorEnteringSPDesign = ESPDTextBox.Text;
    string motorEnteringSPActual = ESPATextBox.Text;
    string motorSuctionSPDesign = SSPDTextBox.Text;
    string motorSuctionSPActual = SSPATextBox.Text;
    string motorDischargeSPDesign = DSPDTextBox.Text;
    string motorDischargeSPActual = DSPATextBox.Text;
    string motorPreheatCoilSPDesign = PCSPDTextBox.Text;
    string motorPreheatCoilSPActual = PCSPATextBox.Text;
    string motorCoolingCoilSPDesign = CCSPDTextBox.Text;
    string motorCoolingCoilSPActual = CCSPATextBox.Text;
    string motorReheatCoilSPDesign = RCSPDTextBox.Text;
    string motorReheatCoilSPActual = RCSPATextBox.Text;
    string motorFilterSPDesign = FSPDTextBox.Text;
    string motorFilterSPActual = FSPATextBox.Text;
    string motorAfterFilterSPDesign = AFSPDTextBox.Text;
    string motorAfterFilterSPActual = AFSPATextBox.Text;
    string motorWheelSPDesign = WSPDTextBox.Text;
    string motorWheelSPActual = WSPATextBox.Text;
    string remarks = RemarksTextArea.Text;

    string updateStatement = @"UPDATE ahu_data SET make=@UNITMAKE, model=@UNITMODEL, type=@UNITTYPE, size=@UNITSIZE, serial=@UNITSERIAL, arrangement=@UNITARRANGEMENT, 
                             class=@UNITCLASS, discharge=@UNITDISCHARGE, make_sheave=@UNITMS, sheave_diameter=@UNITSD, sheave_bore=@UNITSB, 
                             belts=@UNITBELTNUMBER, belt_size=@UNITBELTSIZE, filters=@UNITFILTERNUMBER, filter_size=@UNITBELTSIZE, 
                             unitTotalCFMDesign=@UNITTOTALCFMDESIGN, unitTotalCFMActual=@UNITTOTALCFMACTUAL, unitReturnAirCFMDesign=@UNITRETURNAIRCFMDESIGN, 
                             unitReturnAirCFMActual=@UNITRETURNAIRCFMACTUAL, unitOutsideAirCFMDesign=@UNITOUTSIDEAIRCFMDESIGN, 
                             unitOutsideAirCFMActual=@UNITOUTSIDEAIRCFMACTUAL, unitExhaustAirCFMDesign=@UNITEXHAUSTAIRCFMDESIGN, 
                             unitExhaustAirCFMActual=@UNITEXHAUSTAIRCFMACTUAL, unitFanRPMDesign=@UNITFANRPMDESIGN, 
                             unitFanRPMActual=@UNITFANRPMACTUAL, unitMotorRPMDesign=@UNITMOTORRPMDESIGN, unitMotorRPMActual=@UNITMOTORRPMACTUAL, 
                             unitMotorVoltsDesign=@UNITMOTORVOLTSDESIGN, unitMotorVoltsActual=@UNITMOTORVOLTSACTUAL, unitMotorAmpsDesign=@UNITMOTORAMPSDESIGN, 
                             unitMotorAmpsActual=@UNITMOTORAMPSACTUAL, motor_make=@MOTORMAKE, motor_frame=@MOTORFRAME, motor_hp=@MOTORHP, 
                             motor_rpm=@MOTORRPM, motor_volts=@MOTORVOLTS, motor_phasehz=@MOTORPHASEHZ, motor_fl_amps=@MOTORFULLLOADAMPS, 
                             motor_sf=@MOTORSF, motor_make_sheave=@MOTORMAKESHEAVE, motor_sheave_diameter=@MOTORSHEAVEDIAMETER, 
                             motor_sheave_bore=@MOTORSHEAVEBORE, motor_operating_diameter=@MOTOROPERATINGDIAMETER, motor_sheave_center_distance=@MOTORSHEAVECDISTANCE, 
                             motorTotalSPDesign=@MOTORTOTALSPDESIGN, motorTotalSPActual=@MOTORTOTALSPACTUAL, motorEnteringSPDesign=@MOTORENTERINGSPDESIGN, 
                             motorEnteringSPActual=@MOTORENTERINGSPACTUAL, motorSuctionSPDesign=@MOTORSUCTIONSPDESIGN, motorSuctionSPActual=@MOTORSUCTIONSPACTUAL, 
                             motorDischargeSPDesign=@MOTORDISCHARGESPDESIGN, motorDischargeSPActual=@MOTORDISCHARGESPACTUAL, motorPreheatCoilSPDesign=@MOTORPREHEATCOILSPDESIGN, 
                             motorPreheatCoilSPActual=@MOTORPREHEATCOILSPACTUAL, motorCoolingCoilSPDesign=@MOTORCOOLINGCOILSPDESIGN, motorCoolingCoilSPActual=@MOTORCOOLINGCOILSPACTUAL, 
                             motorReheatCoilSPDesign=@MOTORREHEATCOILSPDESIGN, motorReheatCoilSPActual=@MOTORREHEATCOILSPACTUAL, motorFilterSPDesign=@MOTORFILTERSPDESIGN, 
                             motorFilterSPActual=@MOTORFILTERSPACTUAL, motorAfterFilterSPDesign=@MOTORAFTERFILTERSPDESIGN, motorAfterFilterSPActual=@MOTORAFTERFILTERSPACTUAL, 
                             motorWheelSPDesign=@MOTORWHEELSPDESIGN, motorWheelSPActual=@MOTORWHEELSPACTUAL, remarks=@REMARKS WHERE unit_ID = " + reportID;
    string sqlConnectionString = "Removed for Security";

    using (SqlConnection connection1 = new SqlConnection(sqlConnectionString))
    {
            connection1.Open();

            using (SqlCommand updateCommand = new SqlCommand(updateStatement, connection1))
            {
                    updateCommand.Parameters.AddWithValue("@UNITMAKE", unitMake);
                    updateCommand.Parameters.AddWithValue("@UNITMODEL", unitModel);
                    updateCommand.Parameters.AddWithValue("@UNITTYPE", unitType);
                    updateCommand.Parameters.AddWithValue("@UNITSIZE", unitSize);
                    updateCommand.Parameters.AddWithValue("@UNITSERIAL", unitSerial);
                    updateCommand.Parameters.AddWithValue("@UNITARRANGEMENT", unitArrangement);
                    updateCommand.Parameters.AddWithValue("@UNITCLASS", unitClass);
                    updateCommand.Parameters.AddWithValue("@UNITDISCHARGE", unitDischarge);
                    updateCommand.Parameters.AddWithValue("@UNITMS", unitMS);
                    updateCommand.Parameters.AddWithValue("@UNITSD", unitSD);
                    updateCommand.Parameters.AddWithValue("@UNITSB", unitSB);
                    updateCommand.Parameters.AddWithValue("@UNITBELTNUMBER", unitBeltNumber);
                    updateCommand.Parameters.AddWithValue("@UNITBELTSIZE", unitBeltSize);
                    updateCommand.Parameters.AddWithValue("@UNITFILTERNUMBER", unitFilterNumber);
                    updateCommand.Parameters.AddWithValue("@UNITFILTERSIZE", unitFilterSize);
                    updateCommand.Parameters.AddWithValue("@UNITTOTALCFMDESIGN", unitTotalCFMDesign);
                    updateCommand.Parameters.AddWithValue("@UNITTOTALCFMACTUAL", unitTotalCFMActual);
                    updateCommand.Parameters.AddWithValue("@UNITRETURNAIRCFMDESIGN", unitReturnAirCFMDesign);
                    updateCommand.Parameters.AddWithValue("@UNITRETURNAIRCFMACTUAL", unitReturnAirCFMActual);
                    updateCommand.Parameters.AddWithValue("@UNITOUTSIDEAIRCFMDESIGN", unitOutsideAirCFMDesign);
                    updateCommand.Parameters.AddWithValue("@UNITOUTSIDEAIRCFMACTUAL", unitOutsideAirCFMActual);
                    updateCommand.Parameters.AddWithValue("@UNITEXHAUSTAIRCFMDESIGN", unitExhaustAirCFMDesign);
                    updateCommand.Parameters.AddWithValue("@UNITEXHAUSTAIRCFMACTUAL", unitExhaustAirCFMActual);
                    updateCommand.Parameters.AddWithValue("@UNITFANRPMDESIGN", unitFanRPMDesign);
                    updateCommand.Parameters.AddWithValue("@UNITFANRPMACTUAL", unitFanRPMActual);
                    updateCommand.Parameters.AddWithValue("@UNITMOTORRPMDESIGN", unitMotorRPMDesign);
                    updateCommand.Parameters.AddWithValue("@UNITMOTORRPMACTUAL", unitMotorRPMActual);
                    updateCommand.Parameters.AddWithValue("@UNITMOTORVOLTSDESIGN", unitMotorVoltsDesign);
                    updateCommand.Parameters.AddWithValue("@UNITMOTORVOLTSACTUAL", unitMotorVoltsActual);
                    updateCommand.Parameters.AddWithValue("@UNITMOTORAMPSDESIGN", unitMotorAmpsDesign);
                    updateCommand.Parameters.AddWithValue("@UNITMOTORAMPSACTUAL", unitMotorAmpsActual);
                    updateCommand.Parameters.AddWithValue("@MOTORMAKE", motorMake);
                    updateCommand.Parameters.AddWithValue("@MOTORFRAME", motorFrame);
                    updateCommand.Parameters.AddWithValue("@MOTORHP", motorHP);
                    updateCommand.Parameters.AddWithValue("@MOTORRPM", motorRPM);
                    updateCommand.Parameters.AddWithValue("@MOTORVOLTS", motorVolts);
                    updateCommand.Parameters.AddWithValue("@MOTORPHASEHZ", motorPhaseHz);
                    updateCommand.Parameters.AddWithValue("@MOTORFULLLOADAMPS", motorFullLoadAmps);
                    updateCommand.Parameters.AddWithValue("@MOTORSF", motorSF);
                    updateCommand.Parameters.AddWithValue("@MOTORMAKESHEAVE", motorMakeSheave);
                    updateCommand.Parameters.AddWithValue("@MOTORSHEAVEDIAMETER", motorSheaveDiameter);
                    updateCommand.Parameters.AddWithValue("@MOTORSHEAVEBORE", motorSheaveBore);
                    updateCommand.Parameters.AddWithValue("@MOTOROPERATINGDIAMETER", motorOperatingDiameter);
                    updateCommand.Parameters.AddWithValue("@MOTORSHEAVECDISTANCE", motorSheaveCDistance);
                    updateCommand.Parameters.AddWithValue("@MOTORTOTALSPDESIGN", motorTotalSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORTOTALSPACTUAL", motorTotalSPActual);
                    updateCommand.Parameters.AddWithValue("@MOTORENTERINGSPDESIGN", motorEnteringSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORENTERINGSPACTUAL", motorEnteringSPActual);
                    updateCommand.Parameters.AddWithValue("@MOTORSUCTIONSPDESIGN", motorSuctionSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORSUCTIONSPACTUAL", motorSuctionSPActual);
                    updateCommand.Parameters.AddWithValue("@MOTORDISCHARGESPDESIGN", motorDischargeSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORDISCHARGESPACTUAL", motorDischargeSPActual);
                    updateCommand.Parameters.AddWithValue("@MOTORPREHEATCOILSPDESIGN", motorPreheatCoilSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORPREHEATCOILSPACTUAL", motorPreheatCoilSPActual);
                    updateCommand.Parameters.AddWithValue("@MOTORCOOLINGCOILSPDESIGN", motorCoolingCoilSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORCOOLINGCOILSPACTUAL", motorCoolingCoilSPActual);
                    updateCommand.Parameters.AddWithValue("@MOTORREHEATCOILSPDESIGN", motorReheatCoilSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORREHEATCOILSPACTUAL", motorReheatCoilSPActual);
                    updateCommand.Parameters.AddWithValue("@MOTORFILTERSPDESIGN", motorFilterSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORFILTERSPACTUAL", motorFilterSPActual);
                    updateCommand.Parameters.AddWithValue("@MOTORAFTERFILTERSPDESIGN", motorAfterFilterSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORAFTERFILTERSPACTUAL", motorAfterFilterSPActual);
                    updateCommand.Parameters.AddWithValue("@MOTORWHEELSPDESIGN", motorWheelSPDesign);
                    updateCommand.Parameters.AddWithValue("@MOTORWHEELSPACTUAL", motorWheelSPActual);
                    updateCommand.Parameters.AddWithValue("@REMARKS", remarks);

                    updateCommand.ExecuteNonQuery();
            }

            connection1.Close();
        }
}
如果您不保护
页面加载
不重新执行填充文本框的代码,则会导致这种情况

if (!IsPostBack)
{
    string selectStatement = "SELECT * FROM ahu_data WHERE unit_ID = " + reportID;
    string sqlConnectionString = "Removed for Security";
    using (SqlConnection connection1 = new SqlConnection(sqlConnectionString))
    {
      .... rest of code that pre-fill your fields
是页面的一个布尔属性,如果页面是第一次调用,或者是由于某些需要在服务器端处理的事件而调用,则会通知代码。
在后一种情况下,您不应再次执行填充文本框的代码,否则,当流到达按钮代码时,您将发现文本框具有原始值,而不是修改后的值,因为
页面加载
已重置所有内容

不要忘了上面关于参数化第一个查询的评论。您已经完成了参数化更新的大部分工作,只剩下一个参数,它就完成了。

如果您不保护
页面加载
以避免重新执行填充文本框的代码,就会导致这种情况

if (!IsPostBack)
{
    string selectStatement = "SELECT * FROM ahu_data WHERE unit_ID = " + reportID;
    string sqlConnectionString = "Removed for Security";
    using (SqlConnection connection1 = new SqlConnection(sqlConnectionString))
    {
      .... rest of code that pre-fill your fields
是页面的一个布尔属性,如果页面是第一次调用,或者是由于某些需要在服务器端处理的事件而调用,则会通知代码。
在后一种情况下,您不应再次执行填充文本框的代码,否则,当流到达按钮代码时,您将发现文本框具有原始值,而不是修改后的值,因为
页面加载
已重置所有内容


不要忘了上面关于参数化第一个查询的注释。您已经完成了参数化更新的大部分工作,只剩下一个参数,它就完成了。

更新工作正常,问题是它使用的数据与表中已经存在的数据相同,因此不会更改任何内容

当您单击“保存”按钮时,页面会回发以在服务器上运行代码。首先运行
页面加载
事件并加载原始数据,以替换您在表单中输入的数据。然后运行更新记录的
保存报告按钮
事件

要防止
页面加载
事件处理程序替换表单中的数据,应使用检查页面是否由于回发而加载:

if (!IsPostBack) {
  // load the data from the database in here
}

更新工作正常,问题是它使用的数据与表中已经存在的数据相同,因此不会更改任何内容

当您单击“保存”按钮时,页面会回发以在服务器上运行代码。首先运行
页面加载
事件并加载原始数据,以替换您在表单中输入的数据。然后运行更新记录的
保存报告按钮
事件

要防止
页面加载
事件处理程序替换表单中的数据,应使用检查页面是否由于回发而加载:

if (!IsPostBack) {
  // load the data from the database in here
}

无关的,但第一个选择需要参数化,作为当前您的原始用户输入到数据库中,您也应该考虑将连接连接到配置文件,而不是在每个页面上的硬编码字符串。如果这种情况发生变化,那么您需要进行长时间的更新过程。要将参数化为当前将原始用户输入到数据库中的参数,您还应该考虑将连接移动到配置文件而不是每页上的硬编码字符串。如果这种情况发生变化,则需要进行长时间的更新过程。@ Alxkk。什么是参数化的?