I get errors when returned data from the method using web services references

I write my web services (asmx) as

[WebMethod]
//public List<Ahlam> GetBloodGroup(String gblood)
public DataSet GetBloodGroup(string gblood)
{
        string bloodgroup = gblood.ToString();
        string ApoloConn = ConfigurationManager.ConnectionStrings["ConnSApolo"].ConnectionString;

        SqlConnection con = new SqlConnection(ApoloConn);

        SqlCommand cmd = new SqlCommand("select name as 'NAME', blood_group as 'BLOOD GROUP', gender as 'GENDER' , city as 'CITY' , mobile as 'MOBILE' " +
            "from dontable where blood_group = @bloodgroup", con);
        cmd.Parameters.AddWithValue("@bloodgroup", bloodgroup);
        SqlDataAdapter da = new SqlDataAdapter();

        da.SelectCommand = cmd;
        DataSet ds1 = new DataSet();
        da.Fill(ds1);

        return ds1;
 }

And this is the buttonAction when it's pressed to call the function in my web service

protected void search_bloodgroup_Click(object sender, EventArgs e)
{ 
      string bg = bloodlist.SelectedItem.Text;
      AhlamQueryGroup.AhlamQueryBloodSoapClient client = new 
      AhlamQueryGroup.AhlamQueryBloodSoapClient();

      DataSet set = client.GetBloodGroup(bg);
      gridview_Donate.DataSource = set;
}

This is in the web.config of my project

<connectionStrings>
    <add name="ConnSApolo" 
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ApoloDB.mdf;Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

And this is the web.config in my web services project

<connectionStrings>
    <add name="ConnSApolo" 
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ApoloDB.mdf;Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

When I press the button I get this error

System.Web.Services.Protocols.SoapException: Server was unable to process request.

System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file E:\Ahlam\BloodHospital_project\Blood_Hospital_banks_project\WebServices\WebServices\App_Data\ApoloDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, BooleanapplyTransientFaultHandling)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) at WebServices.AhlamQueryBlood.GetBloodGroup(String gblood) in E:\Ahlam\BloodHospital_project\Blood_Hospital_banks_project\WebServices\WebServices\AhlamQueryBlood.asmx.cs:line 40 --- End of inner exception stack trace --- Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ServiceModel.FaultException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file E:\Ahlam\BloodHospital_project\Blood_Hospital_banks_project\WebServices\WebServices\App_Data\ApoloDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) at WebServices.AhlamQueryBlood.GetBloodGroup(String gblood) in E:\Ahlam\BloodHospital_project\Blood_Hospital_banks_project\WebServices\WebServices\AhlamQueryBlood.asmx.cs:line 40 --- End of inner exception stack trace ---

Source Error:

Line 53: Line 54: public System.Data.DataSet GetBloodGroup(string gblood) { Line 55: return base.Channel.GetBloodGroup(gblood); Line 56: } Line 57:

Source File: E:\Ahlam\BloodHospital_project\Blood_Hospital_banks_project\BloodBank\BloodBank\Connected Services\AhlamQueryGroup\Reference.cs Line: 55

Stack Trace:

[FaultException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file E:\Ahlam\BloodHospital_project\Blood_Hospital_banks_project\WebServices\WebServices\App_Data\ApoloDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) at WebServices.AhlamQueryBlood.GetBloodGroup(String gblood) in E:\Ahlam\BloodHospital_project\Blood_Hospital_banks_project\WebServices\WebServices\AhlamQueryBlood.asmx.cs:line 40 --- End of inner exception stack trace ---]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +153
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +336
BloodBank.AhlamQueryGroup.AhlamQueryBloodSoap.GetBloodGroup(String gblood) +0
BloodBank.AhlamQueryGroup.AhlamQueryBloodSoapClient.GetBloodGroup(String gblood) in E:\Ahlam\BloodHospital_project\Blood_Hospital_banks_project\BloodBank\BloodBank\Connected Services\AhlamQueryGroup\Reference.cs:55
BloodBank.FilterDonor.search_bloodgroup_Click(Object sender, EventArgs e) in E:\Ahlam\BloodHospital_project\Blood_Hospital_banks_project\BloodBank\BloodBank\FilterDonor.aspx.cs:40 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9669714
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3562at

this is the reference of my webreferences in the my project enter image description here this is the web services in the webservices project in the same solution enter image description here

I tried to remove connectionString from web.config in my webservices project but I get another error enter image description here when I press continue I get the same first error

1 answer

  • answered 2018-11-08 00:57 Vijunav Vastivch

    To Simulate the issue:

    First thing you have to do is..

    Create new normal project (Not a web service project) then run your function if it is working or not.

    public DataSet GetBloodGroup(string gblood)
    {
            string bloodgroup = gblood.ToString();
            string ApoloConn = ConfigurationManager.ConnectionStrings["ConnSApolo"].ConnectionString;
    
            SqlConnection con = new SqlConnection(ApoloConn);
    
            SqlCommand cmd = new SqlCommand("select name as 'NAME', blood_group as 'BLOOD GROUP', gender as 'GENDER' , city as 'CITY' , mobile as 'MOBILE' " +
                "from dontable where blood_group = @bloodgroup", con);
            cmd.Parameters.AddWithValue("@bloodgroup", bloodgroup);
            SqlDataAdapter da = new SqlDataAdapter();
    
            da.SelectCommand = cmd;
            DataSet ds1 = new DataSet();
            da.Fill(ds1);
    
            return ds1;
     }
    

    Something like:

    DataSet dsSimulate = new DataSet();
    dsSimulate = GetBloodGroup("YourbloodValueHere");
    

    Then run it in debug mode to get and check the error.

    Your error occur and it says (You already has a database name). Try to check if it is exists or not.

    Reminder:

    Before you do to use and run your Web Service, you have to make sure if it is working in Normal Debug mode.

    There are possible two ways of problem.. It's either your function has an error or the deployment itself upon calling your Web Service.

    when Adding Web Service into your project reference:

    Web.Config File(Project Calling the Web Service) It must look like this after you done adding the service reference deployed link at your project.

    <client>
          <endpoint address="http://localhost:1750/Service1.asmx" binding="basicHttpBinding"
            bindingConfiguration="Service1Soap" contract="Service1.Service1Soap"
            name="Service1Soap" />
          <endpoint address="http://localhost:1750/Service1.asmx" binding="customBinding"
            bindingConfiguration="Service1Soap12" contract="Service1.Service1Soap"
            name="Service1Soap12" />
        </client>
    

    In you Code Behind:

     Service1.Service1SoapClient cli;
    
     protected void Page_Load(object sender, EventArgs e)
     {
        cli = new Service1.Service1SoapClient("Service1Soap12");
        cli.GetBloodGroup("BloodGourHere");//this line returns your DataSet
     }
    

    Hope it helps.