The stored Procedure and Response, Retriving Datas from the DB

Topics: General Discussion Forum, July and December Releases Forum
Feb 28, 2008 at 7:13 AM
Edited Feb 28, 2008 at 7:19 AM
Hi,
Pardon me if this has been asked somewhere else....

Please let me know if anybody is aware about the things. IF i am goign in a wrong way please tell me the suggestions


One of my stored Procedure,suppose dbo.getcustomer has a return value integer... , if the stored procedure executes and if it gets some values corresponds to the input id, it return integer value 1, else if no customer for particular id exists it return 0.

In this case one of my service operation is GetCustomerByID, which uses this stored procedure. I am mentioning my Operation like this..
GetCustomerResponse GetCategory(GetCustomerRequest request)

GetCustomerResponse is the MC, type is DataContract.
MessageContract
public class GetCustomerResponse
{
private Customer CustomerMembersField;

MessageBodyMember(Order = 0)
public Customer CustomerMembers
{
get { return CustomerMembersField; }
set { CustomerMembersField = value; }
}

}


this DC Customer consist of the fields, namely id,name,age,sex

GetCustomerRequest is the MC, consist of the member CustomerID--->system.int32.

_--------------------------------------------------------------------------------------------------------------

here goes my doubts....
My Customer.cs(Service Implementiaion class looks like this)
public PutCustomerResponse RegisterCustomer(PutCustomerRequest request)
{
//throw new Exception("The method or operation is not implemented.");
//implemetning this after the getcategory
CustomerAdapter adapter = new CustomerAdapter();
return adapter.GetCustomer(request);

}

I have created DataRepository class usign the wizard.

  • I am calling the Customer adapter class from my Service implmentation class,

This be the method in the Adapter Class..

public GetCustomerResponse GetCustomer(GetCustomerRequest request)
{
// throw new Exception("The method or operation is not implemented.");
GetCustomerByID getCustomer = new GetCustomerByID();
Customers customer= getCustomer.GetCustomerByIDAction(request.CustomerID);

GetCustomerResponse response = new GetCustomerResponse ();
response.CustomerMembers = Translators.CustomerTranslator.CustomerToView(customer);
return response;


}

  • From the Adapter class i am calling the Class created in the BL, to perform the function
Here goes my GetCustomerByID Class (int he Bussiness Logic)

public class GetCustomerByID
{
public Customers GetCustomerByIDAction(int categoryId)
{
CustomersRepository repository = new CustomersRepository("Customer_Dept");
Customers result = repository.GetCustomersByCustomerID(customerId);
if (result == null)
{
throw new CustomerNotFoundException(string.Format(CultureInfo.CurrentCulture, "CustomerID: '{0}' not found.", customerID));

}

return result;

}
}

-------------------------------------------------------------------------------------------
using this is it possible to get the details of the CustomerID.
and i want to diaplay it in the client side listbox. How can i display this.. PLease help me up...


NImesh


Feb 28, 2008 at 10:14 AM
You can get from the GetCustomerResponse and from the property 'CustomerMembers' the 'Customer' DC with all your fileds to display on the client.
You may also display it on a listbox using a binding like (using the DataGrid in the generated test client of WSSF):

GetCustomerResponse response = proxy.GetCategory(request);
proxy.Close();
ResultsGrid grid = new ResultsGrid(response.CustomerMembers);
grid.Show(this);

Or simple bind the response.CustomerMembers to your Listview. I assume that Customer is some kind of collection or List like LIst<Customer> where Customer is a Dc with all your fields you want to show.

Feb 28, 2008 at 11:34 AM
Edited Feb 28, 2008 at 11:47 AM
Hi, i tried everything.... but nw i am facing an error, which shows that..

"FaultException `1 was unhandled by user code
The requested database XXXXX is not defined in configuration. "

XXXX be the database.

in the client web.config file there is nothing mentioned as database name or anything.
Is that the problem...
But in the web.config file of the Host Project, i can see the database connection string..

What i have to do in order to solve this....
any valuable suggestions.....

Feb 28, 2008 at 11:48 AM
Youy should have the connection strung defined in the config file of your service host, so in this case is should be in your host application.
You may also check that the connection string name is the same that you are using from your code where you create the DatatBase instance.
Feb 28, 2008 at 1:16 PM
Edited Feb 28, 2008 at 1:40 PM
Hi CF,
Thanks for your immediate response...

I am having only one connection string in my host web.config file..
for your reference i am pasting it here,

<configuration>
<appSettings/>
<connectionStrings>
<add name="Connection" connectionString="Data Source=XXXX\sqlexpress;Initial Catalog=Sample;
Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>

this connection string is used every where in the Project. It is used to create Bussiness Entites, Data Repository Classes through the wizard.

1. CalculationClient proxy = new CalculationClient();
2. proxy.AddInsert(23, 34, 57);

i am getting the error in the second line of the above code... fault exception

Anything else i have to check.....

Nimesh


Feb 28, 2008 at 1:54 PM
The proble is in your service, not in the calling client since you are creating the DB instance in your BL/repository classes so you may have something like:

CustomerRepository repository = new CustomerRepository("Connection");  // where Connection is your connectionString name in your config file.
 
// or you may also have the DB name in your repository class like:
 
Database db = DatabaseFactory.CreateDatabase("Connection");
Feb 28, 2008 at 3:11 PM
Edited Feb 28, 2008 at 4:37 PM
woww..........
thanks... my dear friend....
atlast i got.. i got for what i was searching.....

Huraaahhhh....
My problem was with the Connection.. instead of giving the connection string name i gave database name...
now i changed to connection string name.. and its working fine now....

With lots and lots of thanks
Nimesh
Feb 28, 2008 at 5:00 PM
Change the name of your connection string in your config file like:

<configuration>
<appSettings/>
<connectionStrings>
<add name="Sample" connectionString="Data Source=XXXX\sqlexpress;Initial Catalog=Sample;
Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
Notice, name="Sample"
Feb 29, 2008 at 7:51 AM
Edited Feb 29, 2008 at 7:51 AM
Hi,
I am continuing with this thread....

Procedure to GetAllCustomers form the DB,
in this case i have a MC like GetAllCustomerRequest, here is it necessary that i have to send some request.
if yes, what parameter i have to pass.
Reponse MC can be the DataContract or what ?
here i have to use list for getting all the categories na....

please i need some guidance for this

Nimesh
Feb 29, 2008 at 5:15 PM
In GetAllCustomerRequest you may have props pointing to your DCs or primitives.
You will call your BL actions using the translators to map between your DCs (wrapped in your MC) and your BEs that will use your action.
Then your action may return a List<BE> or whatever you want, always related to BEs.
Here you will use another translator (Me ba a composition of simple translators) that will take each BE in the list and translate to a DC and add it to a new list that will in turn be loaded into your MCResponse class.
Mar 1, 2008 at 9:42 AM
Edited Mar 1, 2008 at 12:51 PM
my customer table has 4 fields, mainly name,age,sex,and id..
here i would like to retrive all the customer names into a dropdownlist.
In this case how can i do this function. i am able to retrive Customer details with the categoryid. But inoder to fetch this i am facing some difficulties...
the difficultie are :
1. What should be the response & request for the Operation Contract ?
2. Also i am calling the CustomerAdapter from the Service Implmentation Class

public UpdateCustomerResponse UpdateCustomer(UpdateCustomerRequest request)
{
//throw new Exception("The method or operation is not implemented.");
CustomerAdapter adapter = new CustomerAdapter();
return adapter.GetAllCustomer();
}

3. From the Customer adapter Class i am calling my GetAllCustomersAction class in the BL.
in this class, i am bit confused what to write

GetAllCustomersAction action = new GetAllCustomersAction();
List<BE> customersAll = new List<BE>(action.GetAllCustomers());

after this i am stuck up....

4. Also in the GetAllCustomersAction class, i am using the Categories Repository class to Perform the GetAllCategories fromt he Db.
but i am facing problem with the translation and all....

5. Also while calling it from the client side, how can i do that tooo ?
6. The return type of the translator class which i have to use for the convertion of BE to DC must be something like

publc List<Customers> CustomerTranslator(Customers from)
{
//Customers is the BE
}


Please help me regarding this issue.

Nimesh












Mar 3, 2008 at 10:28 PM


6. Using that translator (from = BE, return type = DC)

public UpdateCustomerResponse TranslateBetweenListOfCustomersAndUpdateCustomerResponse(List<BusinessEntities.Customer> from)
{
    UpdateCustomerResponse to = new UpdateCustomerResponse ();
    TranslateBetweenCustomerAndCustomer translator = new TranslateBetweenCustomerAndCustomer();
 
    List<DataTypes.Customer> translatedCustomers =
        from.ConvertAll<DataTypes.Customer>(
            delegate(BusinessEntities.Customer customer)
            {
                return translator.CustomerTranslator(customer);
            }
        );
 
    to.Customers = translatedCustomers.ToArray();
    return to;
}
And you may call this translator from your adapter like this:

Adapter(,,,)
{
   ...
   List<BusinessEntities.Customer> result = action.Execute(input);
   return outputTranslator.TranslateBetweenListOfCustomersAndUpdateCustomerResponse(result);
Regarding the client side, you should be fine getting the list from 'UpdateCustomerResponse.Customers' that is actually a List<DataTypes.Customer>