简体   繁体   中英

What's wrong with my IF statement?

I'm creating an auditting table, and I have the easy Insert and Delete auditting methods done. I'm a bit stuck on the Update method - I need to be able to get the current values in the database, the new values in the query parameters, and compare the two so I can input the old values and changed values into a table in the database.

Here is my code:

    protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
    {
        string[] fields = null;
        string fieldsstring = null;
        string fieldID = e.Command.Parameters[5].Value.ToString();
        System.Security.Principal.  WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
        string[] namearray = p.Identity.Name.Split('\\');
        string name = namearray[1];
        string queryStringupdatecheck = "SELECT VAXCode, Reference, CostCentre, Department, ReportingCategory FROM NominalCode WHERE ID = @ID";
        string queryString = "INSERT INTO Audit (source, action, itemID, item, userid, timestamp) VALUES (@source, @action, @itemID, @item, @userid, @timestamp)";
        using (SqlConnection connection = new SqlConnection("con string = deleted for privacy"))
        {
            SqlCommand commandCheck = new SqlCommand(queryStringupdatecheck, connection);
            commandCheck.Parameters.AddWithValue("@ID", fieldID);
            connection.Open();
            SqlDataReader reader = commandCheck.ExecuteReader();
            while (reader.Read())
            {
                for (int i = 0; i < reader.FieldCount - 1; i++)
                {
                    if (reader[i].ToString() != e.Command.Parameters[i].Value.ToString())
                    {
                        fields[i] = e.Command.Parameters[i].Value.ToString() + "Old value: " + reader[i].ToString();
                    }
                    else
                    {

                    }
                }
            }
                fieldsstring = String.Join(",", fields);

                reader.Close();

            SqlCommand command = new SqlCommand(queryString, connection);
            command.Parameters.AddWithValue("@source", "Nominal");
            command.Parameters.AddWithValue("@action", "Update");
            command.Parameters.AddWithValue("@itemID", fieldID);
            command.Parameters.AddWithValue("@item", fieldsstring);
            command.Parameters.AddWithValue("@userid", name);
            command.Parameters.AddWithValue("@timestamp", DateTime.Now);
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                Response.Write(x);
            }
            finally
            {
                connection.Close();
            }
        }
    }

The issue I'm having is that the fields[] array is ALWAYS null. Even though the VS debug window shows that the e.Command.Parameter.Value[i] and the reader[i] are different, the fields variable seems like it's never input into.

Thanks

You never set your fields[] to anything else than null, so it is null when you are trying to access it. You need to create the array before you can assign values to it. Try:

           SqlDataReader reader = commandCheck.ExecuteReader();
           fields = new string[reader.FieldCount]

我真的不明白您在这里做什么,但是如果您要进行审核,为什么不将所有更改与时间戳一起插入审核表呢?

Do fields = new string[reader.FieldCount] so that you have an array to assign to. You're trying to write to null[0] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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