470,625 Members | 2,018 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,625 developers. It's quick & easy.

Problem with Connection based Transaction in SQL Helper class

Getting SQL Exception when trying to implement Connection
based Trasaction using SQL Helper class.

I am using the follwing function to execute my stored
procs:

-=-=-=-
ExecuteScalar(ByVal transaction As SqlTransaction, _
ByVal spName As String, _
ByVal ParamArray parameterValues() As Object)
-=-=-=-

I traced my code to reach at the exception causing line of
code. The exception is caused by
function "DiscoverSpParameterSet" under SQL Helper Class.

-=-=-=-=-
Dim cn As New SqlConnection(connectionString)

Dim cmd As SqlCommand = New SqlCommand(spName, cn)
Dim discoveredParameters() As SqlParameter
Try
cn.Open() ''''' Exception is raised here
-=-=-=-=-

Exception details:
Message "Login failed for user 'UserName'."
Source ".Net SqlClient Data Provider"

StackTrace " at
System.Data.SqlClient.ConnectionPool.GetConnection
(Boolean& isInTransaction)
at
System.Data.SqlClient.SqlConnectionPoolManager.Get PooledCon
nection(SqlConnectionString options, Boolean&
isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
----
----

I found on tracing that "DiscoverSpParameterSet" usage
connection string from transaction object
i.e. "Transaction.Connection.ConnectionString". I noticed
the connection string retrieved from Transaction object
does not contain the "pwd: password;" part and so the
connection can not be openned.

Am I doing anything wrong in my coding? Please help.

Thanks in advance for your time and help.

[SNIP]

Dim trans As SqlTransaction
Dim conn As SqlConnection
conn = New SqlConnection
(ConfigAccess.ConnectionString())
If conn Is Nothing Then
Throw New Exception
ElseIf conn.State <> ConnectionState.Open
Then
conn.Open()
End If
Try
trans = conn.BeginTransaction()
Try
SqlHelper.ExecuteScalar
(trans, "procName1", param1, param2, param3)
SqlHelper.ExecuteScalar
(trans, "procName2", param1, param2, param3)
trans.Commit()
Catch ex As SqlException
trans.Rollback()
Throw ex
Catch ex As Exception
Throw ex
Finally
trans.Dispose()
trans = Nothing
End Try
Catch ex As Exception
Throw ex
Finally
If conn.State <>
ConnectionState.Closed Then
conn.Close()
conn.Dispose()
conn = Nothing
End If
End Try

[/SNIP]

--
Regards,
Rahul Anand

Nov 20 '05 #1
4 3959
Hi Rahul,

I give some answers inline (probably not all), however I think this is more
a question for the newsgroup.

Adonet
<news://msnews.microsoft.com/microsoft.public.dotnet.framework.adonet>

Web interface:

<http://communities2.microsoft.com/co...s/?dg=microsof
t.public.dotnet.framework.adonet>
I hope this helps, however there is a lot of guessing in my message?

Cor
-=-=-=-
ExecuteScalar(ByVal transaction As SqlTransaction, _
ByVal spName As String, _
ByVal ParamArray parameterValues() As Object)
-=-=-=-

I traced my code to reach at the exception causing line of
code. The exception is caused by
function "DiscoverSpParameterSet" under SQL Helper Class.

-=-=-=-=-
Dim cn As New SqlConnection(connectionString)

Dim cmd As SqlCommand = New SqlCommand(spName, cn)
Dim discoveredParameters() As SqlParameter
Try
cn.Open() ''''' Exception is raised here
-=-=-=-=-
When the exception is raised here you have or a wrong connection string, or
not a connection at all. And if so I would in this case first make a
connection with the designer and test that.

Exception details:
Message "Login failed for user 'UserName'."
Source ".Net SqlClient Data Provider"

StackTrace " at
System.Data.SqlClient.ConnectionPool.GetConnection
(Boolean& isInTransaction)
at
System.Data.SqlClient.SqlConnectionPoolManager.Get PooledCon
nection(SqlConnectionString options, Boolean&
isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
----
----

I found on tracing that "DiscoverSpParameterSet" usage
connection string from transaction object
i.e. "Transaction.Connection.ConnectionString". I noticed
the connection string retrieved from Transaction object
does not contain the "pwd: password;" part and so the
connection can not be openned.

Am I doing anything wrong in my coding? Please help.

Thanks in advance for your time and help.

[SNIP]

Dim trans As SqlTransaction
Dim conn As SqlConnection
conn = New SqlConnection
(ConfigAccess.ConnectionString())
This set a new connection not a connection open
If conn Is Nothing Then
Throw New Exception
Therefore is in my opinion the two lines above useless
ElseIf conn.State <> ConnectionState.Open
Then
conn.Open()
End If

Try
trans = conn.BeginTransaction()
Try
SqlHelper.ExecuteScalar
(trans, "procName1", param1, param2, param3)
SqlHelper.ExecuteScalar
(trans, "procName2", param1, param2, param3)
trans.Commit()
Catch ex As SqlException
trans.Rollback()
Throw ex
Catch ex As Exception
Throw ex
Finally
trans.Dispose()
trans = Nothing
End Try
Catch ex As Exception
Throw ex
Finally
If conn.State <>
ConnectionState.Closed Then This is useless again in my opinion.
conn.Close()
conn.Dispose()
conn = Nothing
The conn.Close closes the connection
The conn.Dispose closes the connection and removes the connection string

You have to choise from two of them where the dispose is advices for
networks with more than 100 connections.

Setting it to Nothing is useless after this all, and not advices to do.

End If
End Try

[/SNIP]

--
Regards,
Rahul Anand

Nov 20 '05 #2
Hi Cor,
When the exception is raised here you have or a wrong
connection string, or
not a connection at all. And if so I would in this case
first make a
connection with the designer and test that.


I also think the same.
You are right in your guess...

As I have already stated:
I found on tracing that "DiscoverSpParameterSet" usage
connection string from transaction object
i.e. "Transaction.Connection.ConnectionString". I noticed the connection string retrieved from Transaction object
does not contain the "pwd: password;" part and so the
connection can not be openned.


The problem is basically in SQL Helper (DAAB)

http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnbda/html/daab-rm.asp

It uses Connection string extracted from SqlTransaction
object. But the connection string obtained from a
SqlTransaction object hides the "pwd: password" part. And
hence the connection can not be openned with this
connection string.

If anybody have used connection based transaction using
SQL Helper Class ? Please help...

Or is there any bug in SQL Helper ? Which does not allow
us to do so..

--
Thanks,
Rahul Anand

Nov 20 '05 #3
I found the root cause for this problem:

Since the "Persist Security Info" is false by default, the connection string obtained from SqlTransaction object hides the pwd part

http://msdn.microsoft.com/library/de...connections.as

I have checked my code by appending "Persist Security Info=True" to my connection string, in that case everything works fine

But as it will give access to security-sensitive information I think it is not a good option to do this

Does it mean the SqlHelper class provided as DAAB does not provide a secure method to write connection based transactions under which I can execute two different stored procs

-
Regards
Rahul Anan
----- Rahul Anand wrote: ----

Hi Cor
When the exception is raised here you have or a wrong
connection string, o
not a connection at all. And if so I would in this case
first make
connection with the designer and test that


I also think the same
You are right in your guess..

As I have already stated
I found on tracing that "DiscoverSpParameterSet" usag
connection string from transaction objec
i.e. "Transaction.Connection.ConnectionString". I notice the connection string retrieved from Transaction objec
does not contain the "pwd: password;" part and so th
connection can not be openned


The problem is basically in SQL Helper (DAAB

http://msdn.microsoft.com/library/default.asp
url=/library/en-us/dnbda/html/daab-rm.as

It uses Connection string extracted from SqlTransaction
object. But the connection string obtained from a
SqlTransaction object hides the "pwd: password" part. And
hence the connection can not be openned with this
connection string

If anybody have used connection based transaction using
SQL Helper Class ? Please help..

Or is there any bug in SQL Helper ? Which does not allow
us to do so.

-
Thanks
Rahul Anan
Nov 20 '05 #4
On Thu, 3 Jun 2004 22:56:02 -0700, Rahul Anand wrote:
I found the root cause for this problem:

Since the "Persist Security Info" is false by default, the connection string obtained from SqlTransaction object hides the pwd part.

http://msdn.microsoft.com/library/de...onnections.asp

I have checked my code by appending "Persist Security Info=True" to my connection string, in that case everything works fine.

But as it will give access to security-sensitive information I think it is not a good option to do this.

Does it mean the SqlHelper class provided as DAAB does not provide a secure method to write connection based transactions under which I can execute two different stored procs ?


There is an overload to the ExecuteScalar method that takes a SqlConnection
rather than a connection string. Have you tried creating your own
connection object? Use that connection object to create the Transaction
and then use the ExecuteScalar method overload that takes both objects as
parameters.

Just a thought

--
Chris

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kent Lewandowski | last post: by
7 posts views Thread by Zachary Hilbun | last post: by
5 posts views Thread by Jason Huang | last post: by
4 posts views Thread by Prabhat | last post: by
3 posts views Thread by Sean Shanny | last post: by
reply views Thread by Daniel Crespo | last post: by
2 posts views Thread by timpera2501 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.