Monday, March 26, 2012
Oh those Transaction Headaches!
Perhaps a wise soul can help me here. I have an insert routine for an
ASP.Net application and it works fine, but I decided to test the transaction
rollback capabilities by stopping the SQL server when it was just about to
insert the record (I use a breakpoint and then stop the server). What should
have happened is that the transaction is rolled back and my catch block
response.writes the error which would be something about the network
connection.
Unfortunately, within the catch block when it tries to rollback the
transaction I get another error which states
This SqlTransaction has completed; it is no longer usable
Here is the general look of my code
' *** Use SQL commands to directly insert info into DataSource
Dim strConnection As String
Dim objConnection As SqlConnection
Dim objCommand As New SqlCommand()
Dim objTransaction As SqlTransaction
objConnection = New SqlConnection(Application("strConnection"))
Try
objConnection.Open()
objTransaction = objConnection.BeginTransaction()
' *** Create the Command and set its properties
objCommand.Connection = objConnection
objCommand.Transaction = objTransaction
objCommand.CommandText = "sp_LogInsert2"
objCommand.CommandType = CommandType.StoredProcedure
' Set the various Parameters for the stored Procedure
Blah, Blah, Blah
objCommand.ExecuteNonQuery() ' I stop the SQL server just before it
executes this line
objTransaction.Commit()
Catch objError As Exception
'Error was encountered so roll back all the inserts
objTransaction.Rollback()
' Display error details
Response.write("**** Error while inserting data ****" +
objError.Message + "<br/>" + objError.Source)
Finally
objConnection.Close()
End Try
For some reason many people tell me it has to do with the Try block. Wrox
Professional ASP.Net has many examples of having the BeginTransaction within
the Try block. But just for the sake of argument I moved the Open Connection
and the Begin Transaction outside of the try block. No difference was seen.
Could someone please tell me what I am doing wrong?
DanoI think this is not an ASP.NET question.
The error is telling you, that the transaction is completed. You cannot
rollback (or commit) a transaction that is already complete. It's done.
"Dano" <dtemple7502@.rogers.com> wrote in message
news:P19ub.459$FkK1.315@.twister01.bloor.is.net.cab le.rogers.com...
> Hi all!
> Perhaps a wise soul can help me here. I have an insert routine for an
> ASP.Net application and it works fine, but I decided to test the
transaction
> rollback capabilities by stopping the SQL server when it was just about to
> insert the record (I use a breakpoint and then stop the server). What
should
> have happened is that the transaction is rolled back and my catch block
> response.writes the error which would be something about the network
> connection.
> Unfortunately, within the catch block when it tries to rollback the
> transaction I get another error which states
> This SqlTransaction has completed; it is no longer usable
>
> Here is the general look of my code
>
> ' *** Use SQL commands to directly insert info into DataSource
> Dim strConnection As String
> Dim objConnection As SqlConnection
> Dim objCommand As New SqlCommand()
> Dim objTransaction As SqlTransaction
>
> objConnection = New SqlConnection(Application("strConnection"))
> Try
>
> objConnection.Open()
> objTransaction = objConnection.BeginTransaction()
>
> ' *** Create the Command and set its properties
> objCommand.Connection = objConnection
> objCommand.Transaction = objTransaction
> objCommand.CommandText = "sp_LogInsert2"
>
> objCommand.CommandType = CommandType.StoredProcedure
>
> ' Set the various Parameters for the stored Procedure
> Blah, Blah, Blah
>
> objCommand.ExecuteNonQuery() ' I stop the SQL server just before it
> executes this line
> objTransaction.Commit()
> Catch objError As Exception
>
> 'Error was encountered so roll back all the inserts
> objTransaction.Rollback()
>
> ' Display error details
> Response.write("**** Error while inserting data ****" +
> objError.Message + "<br/>" + objError.Source)
>
> Finally
> objConnection.Close()
> End Try
>
> For some reason many people tell me it has to do with the Try block. Wrox
> Professional ASP.Net has many examples of having the BeginTransaction
within
> the Try block. But just for the sake of argument I moved the Open
Connection
> and the Begin Transaction outside of the try block. No difference was
seen.
> Could someone please tell me what I am doing wrong?
> Dano
you cannot roll the transaction back because you lost the connection and
there is no way to notify sqlserver to rollback (it will on its own). there
are other cases when rollbacks will fail, such as when the transaction did a
rollback on its own.
-- bruce (sqlwork.com)
"Dano" <dtemple7502@.rogers.com> wrote in message
news:P19ub.459$FkK1.315@.twister01.bloor.is.net.cab le.rogers.com...
> Hi all!
> Perhaps a wise soul can help me here. I have an insert routine for an
> ASP.Net application and it works fine, but I decided to test the
transaction
> rollback capabilities by stopping the SQL server when it was just about to
> insert the record (I use a breakpoint and then stop the server). What
should
> have happened is that the transaction is rolled back and my catch block
> response.writes the error which would be something about the network
> connection.
> Unfortunately, within the catch block when it tries to rollback the
> transaction I get another error which states
> This SqlTransaction has completed; it is no longer usable
>
> Here is the general look of my code
>
> ' *** Use SQL commands to directly insert info into DataSource
> Dim strConnection As String
> Dim objConnection As SqlConnection
> Dim objCommand As New SqlCommand()
> Dim objTransaction As SqlTransaction
>
> objConnection = New SqlConnection(Application("strConnection"))
> Try
>
> objConnection.Open()
> objTransaction = objConnection.BeginTransaction()
>
> ' *** Create the Command and set its properties
> objCommand.Connection = objConnection
> objCommand.Transaction = objTransaction
> objCommand.CommandText = "sp_LogInsert2"
>
> objCommand.CommandType = CommandType.StoredProcedure
>
> ' Set the various Parameters for the stored Procedure
> Blah, Blah, Blah
>
> objCommand.ExecuteNonQuery() ' I stop the SQL server just before it
> executes this line
> objTransaction.Commit()
> Catch objError As Exception
>
> 'Error was encountered so roll back all the inserts
> objTransaction.Rollback()
>
> ' Display error details
> Response.write("**** Error while inserting data ****" +
> objError.Message + "<br/>" + objError.Source)
>
> Finally
> objConnection.Close()
> End Try
>
> For some reason many people tell me it has to do with the Try block. Wrox
> Professional ASP.Net has many examples of having the BeginTransaction
within
> the Try block. But just for the sake of argument I moved the Open
Connection
> and the Begin Transaction outside of the try block. No difference was
seen.
> Could someone please tell me what I am doing wrong?
> Dano
Thursday, March 22, 2012
OleDbException: Data type mismatch in criteria expression.
On the INSERT command I get this error: System.Data.OleDb.OleDbException:
Data type mismatch in criteria expression.
I haven't found a solution yet in my research. Any idea what I might be
missing?
**************
Datasource setup as follows:
<asp:AccessDataSource ID="AccessDataSource1"
runat="server"
DataFile="~/BeachFuneral.mdb"
InsertCommand="INSERT INTO [ObitComments]
([ObitId], [CommentBy], [CommentEmail], [CommentRelation],
[CommentApproved], [CommentDateTime], [ObitComment]) VALUES (?, ?, ?, ?, ?,
?, ?)" >
<InsertParameters>
<asp:Parameter Name="ObitId" />
<asp:Parameter Name="CommentBy" />
<asp:Parameter Name="CommentEmail" />
<asp:Parameter Name="CommentRelation" />
<asp:Parameter Name="CommentApproved" />
<asp:Parameter Name="CommentDateTime" />
<asp:Parameter Name="ObitComment" />
</InsertParameters>
</asp:AccessDataSource>
**************
Code to insert as follows:
With Me.AccessDataSource1
.InsertParameters("ObitId").DefaultValue =
Request.QueryString("Id")
.InsertParameters("CommentBy").DefaultValue =
Me.txtFullName.Text
.InsertParameters("CommentEmail").DefaultValue =
Me.txtEmail.Text
.InsertParameters("CommentRelation").DefaultValue =
Me.txtRelationship.Text
.InsertParameters("CommentApproved").DefaultValue = True
.InsertParameters("CommentDateTime").DefaultValue = Now.ToString
.InsertParameters("ObitComment").DefaultValue =
Me.txtComment.Text
End With
Try
Me.AccessDataSource1.Insert()
Catch ex As Exception
Response.Write(ex.ToString)
Exit Sub
Finally
' if ok then move on...
Response.Redirect("Obit.aspx?Id=" & Request.QueryString("Id"))
End TryYou have not identified what data types your database is using, either in
this message, or in your code. However, you are attemtping to insert all
string values (text data type), with the exception of one field
(CommentApproved) which you are inserting a boolean value into. So, unless
all of the fields in the table are text except for that one, you would
certainly get that exception.
HTH,
Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
"Bobby Edward" <test@.test.com> wrote in message
news:OkUo0QofIHA.288@.TK2MSFTNGP02.phx.gbl...
> Using Access db with VS2008 (ASP.NET/VB.NET)...
> On the INSERT command I get this error: System.Data.OleDb.OleDbException:
> Data type mismatch in criteria expression.
> I haven't found a solution yet in my research. Any idea what I might be
> missing?
> **************
> Datasource setup as follows:
> <asp:AccessDataSource ID="AccessDataSource1"
> runat="server"
> DataFile="~/BeachFuneral.mdb"
> InsertCommand="INSERT INTO [ObitComments]
> ([ObitId], [CommentBy], [CommentEmail], [CommentRelation],
> [CommentApproved], [CommentDateTime], [ObitComment]) VALUES (?, ?, ?, ?,
> ?, ?, ?)" >
> <InsertParameters>
> <asp:Parameter Name="ObitId" />
> <asp:Parameter Name="CommentBy" />
> <asp:Parameter Name="CommentEmail" />
> <asp:Parameter Name="CommentRelation"
> />
> <asp:Parameter Name="CommentApproved"
> />
> <asp:Parameter Name="CommentDateTime"
> />
> <asp:Parameter Name="ObitComment" />
> </InsertParameters>
> </asp:AccessDataSource>
> **************
> Code to insert as follows:
> With Me.AccessDataSource1
> .InsertParameters("ObitId").DefaultValue =
> Request.QueryString("Id")
> .InsertParameters("CommentBy").DefaultValue =
> Me.txtFullName.Text
> .InsertParameters("CommentEmail").DefaultValue =
> Me.txtEmail.Text
> .InsertParameters("CommentRelation").DefaultValue =
> Me.txtRelationship.Text
> .InsertParameters("CommentApproved").DefaultValue = True
> .InsertParameters("CommentDateTime").DefaultValue =
> Now.ToString
> .InsertParameters("ObitComment").DefaultValue =
> Me.txtComment.Text
> End With
> Try
> Me.AccessDataSource1.Insert()
> Catch ex As Exception
> Response.Write(ex.ToString)
> Exit Sub
> Finally
> ' if ok then move on...
> Response.Redirect("Obit.aspx?Id=" & Request.QueryString("Id"))
> End Try
>
>
Wednesday, March 21, 2012
OleDbParameters, Pls help...
Miniplast <T-Piece> Welding Machine
But it show me error as below:
Server Error in '/' Application.
------------------------
A potentially dangerous Request.Form value was detected from the client (tb0="...niplast 2 <T-Piece> Welding Ma...").
Description: Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted. This value may indicate an attempt to compromise the security of your application, such as a cross-site scripting attack. You can disable request validation by setting validateRequest=false in the Page directive or in the configuration section. However, it is strongly recommended that your application explicitly check all inputs in this case.
Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (tb0="...niplast 2 <T-Piece> Welding Ma...").
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (tb0="...niplast 2 <T-Piece> Welding Ma...").]
System.Web.HttpRequest.ValidateString(String s, String valueName, String collectionName) +230
System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, String collectionName) +99
System.Web.HttpRequest.get_Form() +121
System.Web.UI.Page.GetCollectionBasedOnMethod() +70
System.Web.UI.Page.DeterminePostBackMode() +47
System.Web.UI.Page.ProcessRequestMain() +2106
System.Web.UI.Page.ProcessRequest() +218
System.Web.UI.Page.ProcessRequest(HttpContext context) +18
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() +179
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +87
Here is my code
Dim strsql as string= "INSERT INTO ToolNAccessories (Name, Descrip, Price) VALUES" & _
"( @dotnet.itags.org.Params0, @dotnet.itags.org.Params1, @dotnet.itags.org.Params2)"
Dim objCmd as New OleDbCommand(StrSql, Conn)
Dim Objparam as OleDbParameterWith ObjCmd.Parameters:
.Add(New OleDbParameter("@dotnet.itags.org.Params0", Tb0.Text))
.Add(New OleDbParameter("@dotnet.itags.org.Params1", Tb1.Text))
.Add(New OleDbParameter("@dotnet.itags.org.Params2", Tb2.Text))
End With
Conn.Open()
ObjCmd.ExecuteNonQuery()
Conn.Close()
This piece of code was working very well until i insert
Miniplast <T-Piece> Welding Machine
This sound like Sql injection... how can it be... im using parameters to prevent it already right?
Any idea what happened? Pls guard me on tis..
Thanks in advance
life's NgOn the page, in the @.page directive, add ValidateRequest="false". This will allow the < and > character in posted data, but you should then check for script injection.
Thanks douglas,
oh my God.... now i then realize there is sooo much injections we gonna deal with... :(
Mr douglas, i dont think im going to set validateRequest="false"... but I having some questions in my mind..
1. Will my previous error let any attacker getting some clues to proceed their job?
2. Can i try some "weird" characters like <>)*&%$^&* for some user's input on any website? is that illegal? i just wish to try out see whether the web application is secure from this aspect.. will that made a web server down or something?
Best Regards
life's Ng
Hi Douglas,
I wonder if i set validateRequest="false" , all i need to check is script injection using "Server.HtmlEncode" is tat correct? Anything else i need to consider?
Regards
life's Ng
If it is acceptable to have users enter < or >, then you will need to set ValidateRequest="false". Failing that, you should think about handling the error in a somewhat more elegant way. I am not totally certain that will address all the issues. Anyone else who has looked at this more closely?