Web Service + SqlConnection

A

axel22

Ok here it goes.
1. I've created a new web service project in F:\c#\WebSites\ServisBaze.
2. I've published the web service with IIS in a virtual directory who's
actual path is:
F:\c#\Published\ServisBaze
3. Tried opening it in a browser - it works perfectly.
4. Now I've added an SQL Database object Database.mdf into my project.
5. I've manually edited the database tables and added data.
6. I've added a webmethod 'izvadiImena' to work with the database.
7. I've published the website - it works like a charm.
8. Now I've changed the source code a little bit, and tried publishing
it again. I get an error:

------ Build started: Project: F:\...\ServisBaze\, Configuration: Debug
..NET ------
Pre-compiling Web Site

Building directory '/ServisBaze/App_Code/'./: Publication (web): The
process cannot access the file
'F:\c#\WebSites\ServisBaze\App_Data\Database.mdf' because it is being
used by another process.

Pre-compilation Complete
------ Skipped Publish: Project F:\...\ServisBaze\, Configuration:
Debug .NET ------

========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped
==========
========== Publish: 0 succeeded, 0 failed, 1 skipped ==========

9. I bang my head against the wall.
10. As write this post down, and some time has passed, I try to publish
it again, and it works.
Subsequent publishing, however, results in the same message.
<br>
<br>

Could anyone explain? Could it be that a connection pool has been
created and
it denies subsequent access to database?
Does anybody know how I can prevent this from happening, so I don't
have to
wait 5 minutes each time I change something in the source code.

<br>
<br>

Here is my source code:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
private class serviceConfiguration
{
public static string connectionString
{
get
{
return
ConfigurationManager.AppSettings["connectionString"];
}
}
}

public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public List<string> nadiImena()
{
List<string> ls = new List<string>();
ls.Add("Zikino ime.");
using (SqlConnection conn = new
SqlConnection(serviceConfiguration.connectionString))
{
try
{
conn.Open();
SqlCommand com = new SqlCommand("SELECT * FROM
tabljic", conn);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
ls.Add((string)reader["ime"]);
}
}
catch (Exception e)
{
ls.Add(e.ToString());
}
finally
{
if (conn != null) conn.Close();
}
}
return ls;
}

}
 
C

Cor Ligthert [MVP]

Axel,

Try to get the SQL server on its IP address instead of the drive and don't
use integrated securitiy.
(First you have to "attach" really of course the database to your SQL server
and don't use the SQLExpress testing method).

Here the page with connectionstrings
http://www.connectionstrings.com/

I hope this helps,

Cor




Ok here it goes.
1. I've created a new web service project in F:\c#\WebSites\ServisBaze.
2. I've published the web service with IIS in a virtual directory who's
actual path is:
F:\c#\Published\ServisBaze
3. Tried opening it in a browser - it works perfectly.
4. Now I've added an SQL Database object Database.mdf into my project.
5. I've manually edited the database tables and added data.
6. I've added a webmethod 'izvadiImena' to work with the database.
7. I've published the website - it works like a charm.
8. Now I've changed the source code a little bit, and tried publishing
it again. I get an error:

------ Build started: Project: F:\...\ServisBaze\, Configuration: Debug
.NET ------
Pre-compiling Web Site

Building directory '/ServisBaze/App_Code/'./: Publication (web): The
process cannot access the file
'F:\c#\WebSites\ServisBaze\App_Data\Database.mdf' because it is being
used by another process.

Pre-compilation Complete
------ Skipped Publish: Project F:\...\ServisBaze\, Configuration:
Debug .NET ------

========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped
==========
========== Publish: 0 succeeded, 0 failed, 1 skipped ==========

9. I bang my head against the wall.
10. As write this post down, and some time has passed, I try to publish
it again, and it works.
Subsequent publishing, however, results in the same message.
<br>
<br>

Could anyone explain? Could it be that a connection pool has been
created and
it denies subsequent access to database?
Does anybody know how I can prevent this from happening, so I don't
have to
wait 5 minutes each time I change something in the source code.

<br>
<br>

Here is my source code:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
private class serviceConfiguration
{
public static string connectionString
{
get
{
return
ConfigurationManager.AppSettings["connectionString"];
}
}
}

public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public List<string> nadiImena()
{
List<string> ls = new List<string>();
ls.Add("Zikino ime.");
using (SqlConnection conn = new
SqlConnection(serviceConfiguration.connectionString))
{
try
{
conn.Open();
SqlCommand com = new SqlCommand("SELECT * FROM
tabljic", conn);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
ls.Add((string)reader["ime"]);
}
}
catch (Exception e)
{
ls.Add(e.ToString());
}
finally
{
if (conn != null) conn.Close();
}
}
return ls;
}

}
 
A

axel22

Do I do that from Visual Studio or from MS SQL Server 2005?
I can only see that it is a service from the Configuration Manager,
but I'll look this up.
Thnx!



Cor Ligthert [MVP] je napisao/la:
Axel,

Try to get the SQL server on its IP address instead of the drive and don't
use integrated securitiy.
(First you have to "attach" really of course the database to your SQL server
and don't use the SQLExpress testing method).

Here the page with connectionstrings
http://www.connectionstrings.com/

I hope this helps,

Cor




Ok here it goes.
1. I've created a new web service project in F:\c#\WebSites\ServisBaze.
2. I've published the web service with IIS in a virtual directory who's
actual path is:
F:\c#\Published\ServisBaze
3. Tried opening it in a browser - it works perfectly.
4. Now I've added an SQL Database object Database.mdf into my project.
5. I've manually edited the database tables and added data.
6. I've added a webmethod 'izvadiImena' to work with the database.
7. I've published the website - it works like a charm.
8. Now I've changed the source code a little bit, and tried publishing
it again. I get an error:

------ Build started: Project: F:\...\ServisBaze\, Configuration: Debug
.NET ------
Pre-compiling Web Site

Building directory '/ServisBaze/App_Code/'./: Publication (web): The
process cannot access the file
'F:\c#\WebSites\ServisBaze\App_Data\Database.mdf' because it is being
used by another process.

Pre-compilation Complete
------ Skipped Publish: Project F:\...\ServisBaze\, Configuration:
Debug .NET ------

========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped
==========
========== Publish: 0 succeeded, 0 failed, 1 skipped ==========

9. I bang my head against the wall.
10. As write this post down, and some time has passed, I try to publish
it again, and it works.
Subsequent publishing, however, results in the same message.
<br>
<br>

Could anyone explain? Could it be that a connection pool has been
created and
it denies subsequent access to database?
Does anybody know how I can prevent this from happening, so I don't
have to
wait 5 minutes each time I change something in the source code.

<br>
<br>

Here is my source code:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
private class serviceConfiguration
{
public static string connectionString
{
get
{
return
ConfigurationManager.AppSettings["connectionString"];
}
}
}

public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public List<string> nadiImena()
{
List<string> ls = new List<string>();
ls.Add("Zikino ime.");
using (SqlConnection conn = new
SqlConnection(serviceConfiguration.connectionString))
{
try
{
conn.Open();
SqlCommand com = new SqlCommand("SELECT * FROM
tabljic", conn);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
ls.Add((string)reader["ime"]);
}
}
catch (Exception e)
{
ls.Add(e.ToString());
}
finally
{
if (conn != null) conn.Close();
}
}
return ls;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top