473,378 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Thread in a asmx page - Problem

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
9 2156
Plater
7,872 Expert 4TB
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
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
7,872 Expert 4TB
HAve you tried making it make "log" entries at various points durring your thread?
like:
"Thread entered"
...
...
...
"Thread exited"
Jan 22 '08 #4
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
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
1,435 Expert 1GB
it might be throwing an exception, have you tried logging the exception message?
Jan 23 '08 #7
Plater
7,872 Expert 4TB
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
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
7,872 Expert 4TB
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

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

Similar topics

6
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something...
1
by: Stevenson | last post by:
Hello. If have written a webservice within C# that is called from an aspx and processes incoming data correctly. The problem is that the ASMX runs twice before returning. I have debugged the...
1
by: Craig G | last post by:
is there any implications of having more than one asmx page in your webservice i was intending to use a new asmx for each of my classes but a colleague in work says there is problems with this....
5
by: Doug Kent | last post by:
Hi, I am using a STA thread to run a COM object. On a couple of machines the thread runs fine. On another machine the thread won't start, and no exceptions are thrown. This code is running...
1
by: PeterW | last post by:
On my machine I developed a WebService on localhost and if I browse the asmx file I get the usual page that defines the webmethods exposed by the webservice and the opportunity to get the wsdl file...
7
by: Kimi | last post by:
I am first time study the .NET, and write a .asmx file as the Book saying. But, when I access this .asmx file through the IE like http://localhost/abc.asmx, I can only open it or download it. I...
3
by: Maxwell2006 | last post by:
Hi, When I run a web service project, ASP.NET shows me a default web method invoke page. How can I disable/modify the default test (or method invoke) page of the ASP.NET web services? ...
0
by: wc_chan | last post by:
I wrote a web service projects having 2 asmx and 1 dataset, says dsTest1. The amsx will have a parameter of the dataset as follows: in 1st asmx: <Wethod()HelloWorld1 (byval a1 as dsTest1) as...
5
by: =?Utf-8?B?a3Jpcw==?= | last post by:
I created a simple webservice .asmx on Visual Studio 2005. As I plan to deploy it to Sharepoint Portal 2003, I copied asmx to \web server extensions\60\ISAPI directory of the server, where all...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.