简体   繁体   English

更新命令神秘地无法在ASP.NET中工作

[英]Update Command Mysteriously not working in ASP.NET

I have an ASP.NET application in which I'll be updating some notes, however the command won't update the database notes. 我有一个ASP.NET应用程序,其中将更新一些注释,但是该命令不会更新数据库注释。 It did it once and then decided not to do it anymore. 它做了一次,然后决定不再做。 No changes were made between in not working, working once and then not working again. 在不工作,一次工作然后再不工作之间未进行任何更改。 Very odd situation... here's the code. 非常奇怪的情况...这是代码。 I can't find anything wrong with it. 我找不到任何问题。

protected void saveNotes_Click(object sender, EventArgs e)
{
     string theNotes = notes.InnerText;
     string[] vars = { "@notes", "@jobNumber" };
     string[] vals = { theNotes, jobNumber };
     HelperMethods.OleDB_Query("UPDATE [Jobs] SET Notes = @notes WHERE Job_Number = @jobNumber", vars, vals);

     test.InnerText = theNotes;
}

I display the notes in the aspx page to verify that they do indeed populate and they do. 我在aspx页面中显示注释,以验证它们确实确实存在并且确实存在。

Here's the database access code: 这是数据库访问代码:

public static void OleDB_Query(string cmd, string[] vars = null, string[] vals = null)
{
    OleDbConnection Connection = new OleDbConnection(connectionString);
    OleDbCommand Command = new OleDbCommand(cmd, Connection);
    if (vars != null) {
        for (int k = 0; k < vars.Length; k++)
            Command.Parameters.AddWithValue(vars[k], vals[k]);
        Command.Connection.Open();
        Command.ExecuteNonQuery();
        Command.Connection.Close();
    }
}

I'm attempting to display the job number and notes on each update attempt. 我正在尝试显示作业号和每次更新尝试的注释。

I'm getting the job number via URL query string. 我通过URL查询字符串获取工作编号。 If the job number starts with a zero, then the zeros are all parsed off and and what's displayed is the notes text followed by the parsed job number. 如果作业号以零开头,则将零全部解析掉,并且显示的是注释文本,后跟解析的作业号。 If the job number doesn't start with a zero, only the job number is displayed. 如果作业编号不是以零开头,则仅显示作业编号。 Makes no sense whatsoever. 毫无意义。 It's a very basic setup that I've done many times in the past. 这是我过去做过很多次的非常基本的设置。 It's just deciding not to work anymore for some odd reason. 它只是出于某种奇怪的原因而决定不再工作。

I would guess it's related to not disposing of the connection and commands properly. 我想这与未正确处理连接和命令有关。 Update your static helper method to use using statements and it should ensure your connections and commands are being cleaned up and ready for the next use. 更新您的静态帮助器方法以使用using语句,它应确保您的连接和命令已被清理并准备下次使用。

public static void OleDB_Query(string cmd, string[] vars = null, string[] vals = null)
{
    using(OleDbConnection Connection = new OleDbConnection(connectionString))
    {
        Command.Connection.Open();
        using(OleDbCommand Command = new OleDbCommand(cmd, Connection))
        {
            if (vars != null) {
                for (int k = 0; k < vars.Length; k++)
                    Command.Parameters.AddWithValue(vars[k], vals[k]);

                Command.ExecuteNonQuery();
        }
        Command.Connection.Close();            
    }
}

Could this have to do with the fact that you are passing your Job_Number as a string? 这可能与您将Job_Number作为字符串传递的事实有关吗? What is the underlying sql datatype? 什么是基础sql数据类型? If it is a char or varchar , stripping off the leading zeros will produce a different Job_Number . 如果是charvarchar ,则去除前导零将产生不同的Job_Number

Consider parsing your QueryString value to an int before passing it to SQL which will strip all leading zeros. 考虑将QueryString值解析为一个int然后再将其传递给SQL,这将除去所有前导零。

TWO PROBLEMS (ONE SOLVED, ONE SORT OF SOLVED): 两个问题(一个解决方案,一个解决方案)

SOLVED: 解决了:

The data wasn't being inputted into the database because the jobNumber variable was being reset on each page load. 未将数据输入数据库,因为在每次页面加载时都会重置jobNumber变量。 I tried stopping in by using (!IsPostBack) but then the variable had no value. 我尝试使用(!IsPostBack)停下来,但是变量没有值。 Since it seemed to be losing its value somehow I just stuck it in ViewState and I was able to successfully update the database. 由于它似乎正在以某种方式失去价值,因此我将其卡在ViewState中,从而能够成功更新数据库。

UNSOLVED (BUT WORK AROUND FOUND) PROBLEM: 未解决(但仍可解决)问题:

When I pass a job number without leading zeros, everything works just fine and dandy. 当我通过一个不带前导零的工作编号时,一切正常。 However, when I pass a job number with leading zeros, the query string parses it and I'm left with a job number that is incorrect. 但是,当我传递带有前导零的作业编号时,查询字符串会对其进行解析,而我留下的作业编号不正确。 I tested the variable just before passing it across the url and just after. 我在将变量传递给url之前和之后进行了测试。 It's being parsed in the url. 正在网址中进行解析。 Anyone know anything about this? 有人知道这是什么一回事吗? I'm thinking I'll just pass an extra variable which represents the number of leading zeros so I can stick 'em back on. 我想我将只传递一个额外的变量,该变量代表前导零的数量,因此我可以继续使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM