[英]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. 我对Update方法有些困惑-我需要能够获取数据库中的当前值,查询参数中的新值,并进行比较,以便可以将旧值和更改后的值输入到表中在数据库中。
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. 我遇到的问题是,fields []数组始终为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. 即使VS调试窗口显示e.Command.Parameter.Value [i]和reader [i]不同,fields变量似乎也从未输入过。
Thanks 谢谢
You never set your fields[]
to anything else than null, so it is null when you are trying to access it. 您永远不会将fields[]
设置为null以外的任何其他内容,因此在尝试访问它时,它为null。 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. 是否将fields = new string[reader.FieldCount]
设置为一个数组。 You're trying to write to null[0]
. 您正在尝试写入null[0]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.