By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 775 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

Thread in a asmx page - Problem

P: 5
Hello,
I have a web service method that need to create a thread that does some work in the background so the main process can go on. The work in the background is a simple insert into a database. If I try this code in an application console works, but when I try this code inside the asmx page doesnt work. I dont get any error, just the data are not inserted into the database. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. <%@ WebService Language="VB" Class="TenantService" %> 
  2. Imports System 
  3. Imports System.Web.Services 
  4. Imports System.Data.SqlClient
  5. Imports System.ComponentModel
  6.  
  7. Public Class TenantService : Inherits WebService   
  8.     Public Code As String
  9.         Public FirstName As String
  10.         Public LastName As String
  11.         Public Data As String
  12.         Public Charge As String
  13.  
  14.     <WebMethod()> _
  15.     Public Function ConnectToSql(Code As String, FirstName as String, LastName as String, Data as String, Charge as String) as String
  16.     Dim conn As New SqlConnection
  17.     Dim myCommand As SqlCommand
  18.     Dim dr As String
  19.     Dim r As SqlDataReader
  20.  
  21.     conn.ConnectionString = _
  22.     "integrated security=SSPI;data source=FSECCIA-DT;" & _
  23.     "persist security info=False;initial catalog=senior600614sp2"
  24.     Try
  25.         conn.Open()
  26.  
  27.         myCommand = New SqlCommand("Select * from webservicetable WHERE FirstName = '" & FirstName & "' And LastName =  '" & LastName & "' And Date = '" & Data & "' And Charge = '" & Charge & "'", conn)
  28.         r = myCommand.ExecuteReader()
  29.         if (r.Read())
  30.             r.close()
  31.             return "Data already inserted"
  32.         else
  33.             r.close()
  34.             Dim AreaObject As New UpdateDBClass
  35.             Dim newThread As System.Threading.Thread = new System.Threading.Thread (AddressOf AreaObject.updateDB)
  36.             AreaObject.Code=Code
  37.             AreaObject.FirstName=FirstName
  38.             AreaObject.LastName=LastName
  39.             AreaObject.Data=Data
  40.             AreaObject.Charge=Charge
  41.  
  42.             newThread.IsBackground = true
  43.  
  44.             newThread.Start()
  45.  
  46.             return "Success"
  47.         end if
  48.  
  49.     Catch ex As Exception
  50.         return "Exception - Try to choose another code"
  51.     Finally
  52.         conn.Close()
  53.     End Try
  54. End Function
  55.  
  56. End Class
  57.  
  58. Public Class UpdateDBClass
  59.  
  60.         Public Code As String
  61.         Public FirstName As String
  62.         Public LastName As String
  63.         Public Data As String
  64.         Public Charge As String
  65.  
  66.         Dim myCommand As SqlCommand
  67.  
  68.         Public conn As New SqlConnection
  69.  
  70.         Sub updateDB()
  71.             conn.ConnectionString = _
  72.             "integrated security=SSPI;data source=FSECCIA-DT;" & _
  73.             "persist security info=False;initial catalog=senior600614sp2"
  74.             Try
  75.                 conn.Open()
  76.  
  77.                 myCommand = New SqlCommand("insert into webservicetable values ('" & Code & "', '" & FirstName & "', '" & LastName & "', '" & Data & "', '" & Charge & "')", conn)
  78.                 'myCommand = New SqlCommand("insert into webservicetable values ('5555', 'hhh', 'tttt', '', '')", conn)
  79.  
  80.                 myCommand.ExecuteNonQuery()
  81.             Catch ex As Exception
  82.             Finally
  83.                 conn.Close()
  84.             End Try
  85.  
  86.         End Sub
  87.  
  88.     End Class
  89.  
Do you have any idea?
Thank you
Jan 22 '08 #1
Share this Question
Share on Google+
9 Replies


Plater
Expert 5K+
P: 7,872
Your UpdateDBClass function there has a try/catch where nothing is done with an Exception (The compiler should give you a warning about that)
I would guess that it's throwing a permisions error because the user that the service runs as does not have permision to carry out the db actions.
Jan 22 '08 #2

P: 5
Thanks for your answer. The problem is this. If for example I put the method "updateDB()" of UpdateDBClass inside my TenantService class and I exposed it as a web service method with WebMethod (like ConnectSql method),
it works. So, the code is correct, but It seems that the thread is not created or something....
Jan 22 '08 #3

Plater
Expert 5K+
P: 7,872
HAve you tried making it make "log" entries at various points durring your thread?
like:
"Thread entered"
...
...
...
"Thread exited"
Jan 22 '08 #4

P: 5
I tried to call the methos isAlive of the thread and return the value instead of "success" of my web method.
The return value is true. If I put a sleep of 3-5 sec in the main proc before the return, then the return value is false.
I really dont understand.
Jan 22 '08 #5

P: 5
Hello,
After the debugging, I figured out the the thread exits exactly when the thread tries to open the connection to the db (conn.open) in the updateDB method.

What should I do?
Jan 23 '08 #6

Shashi Sadasivan
Expert 100+
P: 1,435
it might be throwing an exception, have you tried logging the exception message?
Jan 23 '08 #7

Plater
Expert 5K+
P: 7,872
it might be throwing an exception, have you tried logging the exception message?
Yeah, I second (well third since I said it earlier too :-P)
Jan 23 '08 #8

P: 5
You are right guys. thank you for that. But I still dont understand.
Here is the exception thrown:

System.Data.SqlClient.SqlException: Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.
at System.Data.SqlClient.ConnectionPool.GetConnection (Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnectionPoolManager.Get PooledConnection(SqlConnectionString options, Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
at UpdateDBClass.updateDB() in C:\Voyager60\pages\Example.asmx:line 80

Why the first select in the web service methos is done, whereas the insert in the thread not? The string to connect to the db
conn.ConnectionString = _
"integrated security=SSPI;data source=FSECCIA-DT;" & _
"persist security info=False;initial catalog=senior600614sp2"

is the same. Why does teh login fail in the thread???
Jan 23 '08 #9

Plater
Expert 5K+
P: 7,872
Seems like the thread does not have a user associated with it. Not sure why. You could use a non-windows login for that thread's connectionstring, but you would have to make sure to create the login on the SQL server and give the correct permissions.
Jan 23 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.