In my understanding, the execute nonquery command in C# SQLCommand has to return the number of affected records, I have used this SQLCommand with SQL Transaction, it return a negative one but when I check the database the record has been inserted.
What I want to actually achieve is, to commit only if a record has been inserted into the database successfully, now I tried to achieved this by checking the return value of the executeNoneQuery command but as like I said it returns a negative one, to me that says the query was perhaps unsuccessfully however the database records seems to suggest otherwise.
Is there somewthing that I am missing here,am I perhaps implementing my SQlCommand and SQLTransaction incorrectly
Here is my current code
using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.AppSettings["SQLConnString"]))
{
sqlConn.Open();
using (sqlTrans = sqlConn.BeginTransaction())
{
using (SqlCommand command = new SqlCommand("dbo.sp_InsertWizardImport", sqlConn, sqlTrans))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@GL_ACC", GL_ACC));
command.Parameters.Add(new SqlParameter("@DOC_REF_NUM ", DOC_REF_NUM));
int record = command.ExecuteNonQuery();
sqlTrans.Commit();
str2 = _reader.ReadLine();
}
}
}
There is of course more variables to the stored prod but I only included 2 and other peace of of code that I think is problematic