I am trying to update some fields in the database, using ExecuteNonQuery().
When trying to update, i receive no error or warning, and it returns me one affected row count.
The thing is, that i can only for the current request, see the change. If i reload the page or go through SMSS, the changes are not shown in the database.
Hes a little code.
string Sql = "UPDATE AccessUser SET [" + Field + "] = '" + Database.SqlEscapeInjection(Value) + "' WHERE AccessUserID = " + User.ID;
Response.Write("<br>" + Sql + "<br>");
IDataReader Se = Database.CreateDataReader("SELECT * FROM AccessUser WHERE AccessUserID = " + User.ID);
while (Se.Read())
{
Response.Write((string)Se[Field] + "<br>");
}
int affectedrows = Database.ExecuteNonQuery(Sql, Database.CreateConnection());
Response.Write("<br>NUMMER: " + affectedrows + "<br>");
IDataReader S = Database.CreateDataReader("SELECT * FROM AccessUser WHERE AccessUserID = " + User.ID);
while (S.Read())
{
Response.Write((string)S[Field] + "<br>");
}
As you see, i first output the current value, afterwards i run the update query, and afterwards again output the value. ( Must notify you, that i usually do use bound parameters, but for the sake of debugging i threw them away and made this. )
The result is as follow: Jonas NUMMER: 1 Mikkel
When i refresh the page, or look in the database, the row hasn't changed though. What to do? :D
EDIT: The documentation for the system "Dynamicweb-CMS" states that its not transactional: http://developer.dynamicweb-cms.com/api8/Dynamicweb~Dynamicweb.Database~ExecuteNonQuery(String,IDbConnection,IDbTransaction).html And that i should just be able to do, as i actually do. Weird stuff.
EDIT 2: To please those with a "bound parameters OCD"
using (IDbConnection connection = Database.CreateConnection())
{
IDbCommand command = connection.CreateCommand();
command.CommandText = "UPDATE AccessUser SET [" + Field + "] = @ParamValue WHERE AccessUserID = @UserId";
Database.AddStringParam(command, "@ParamValue", Value);
Database.AddInt32Param(command, "@UserId", User.ID);
//Execute command
command.ExecuteNonQuery();
connection.Close();
}
Same result as with the other query method.
Tried this as well. Nothing happens.
using (SqlConnection cn = new SqlConnection(SQLString))
{
SqlTransaction Trans;
try
{
cn.Open();
Trans = cn.BeginTransaction();
IDbCommand command = cn.CreateCommand();
command.Transaction = Trans;
command.CommandText = "UPDATE AccessUser SET [" + Field + "] = @ParamValue WHERE AccessUserID = @UserId";
Database.AddStringParam(command, "@ParamValue", Value);
Database.AddInt32Param(command, "@UserId", User.ID);
//Execute command
int affectedRecords = command.ExecuteNonQuery();
Response.Write("<br>NUMMER: " + affectedRecords + "<br>");
Response.Write(cn.ServerVersion );
Trans.Commit();
cn.Close();
}
catch (Exception e)
{
Response.Write("<span style='color:red;'>" + e.Message + "</span>");
}
};
I ran the profiler and see the update statement. After the update statement, the following is sent
Audit Login:
-- network protocol: LPC
set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed
Which, in my logic, tells me that the change is committed. Now, however, afterwards, i get this (Had to cut it down, Stackoverflow did not like it)
RPC:Completed:
exec sp_executesql N'UPDATE [AccessUser] SET [AccessUserUpdatedOn] = @p1 WHERE (([AccessUserID] = @p2) AND ((@p3 = 1 AND [AccessUserParentID] IS NULL) OR ([AccessUserParentID] = @p4)) AND ((@p5 = 1 AND [AccessUserUserName] IS NULL) OR ([AccessUserUserName] = @p6)) AND ((@p7 = 1 AND [AccessUserPassword] IS NULL) OR ([AccessUserPassword] = @p8)) AND ((@p9 = 1 AND [AccessUserName] IS NULL) OR ([AccessUserName] = @p10)) AND ((@p11 = 1 AND [AccessUserDepartment] IS NULL) OR ([AccessUserDepartment] = @p12)) AND ((@p13 = 1 AND [AccessUserEmail] IS NULL) OR ([AccessUserEmail] = @p14)) AND ((@p15 = 1 AND [AccessUserPhone] IS NULL) OR ([AccessUserPhone] = @p16)) AND ((@p17 = 1 AND [AccessUserFax] IS NULL) OR ([AccessUserFax] = @p18)) AND ((@p19 = 1 AND [AccessUserGroups] IS NULL) OR ([AccessUserGroups] = @p20)) AND ((@p21 = 1 AND [AccessUserType] IS NULL) OR ([AccessUserType] = @p22)) AND ((@p23 = 1 AND [AccessUserValidFrom] IS NULL) OR ([AccessUserValidFrom] = @p24)) AND ((@p25 = 1 AND [AccessUserValidTo] IS NULL) OR ([AccessUserValidTo] = @p26)) AND ((@p27 = 1 AND
-and updates the content back to the original again. How would i check if this is some sort of transactional rollback? Or anything else. Pardon the English.