Solved

return instance by data source type

Posted on 2014-02-16
2
289 Views
Last Modified: 2014-02-17
hi there i hope that someone here will understand my problem
i have a grid that gets the data by entity framework using stored PROCEDURE
the SP called :GetAllCustomers_Result() and its return a list of all of my customers
its working fine

now i have another SP that return only the Active Customers both are working fine
and retruning the same filds the difference is only in the rows count that returns

when the data is on the grid i have a button that open the new customer form
i can insert a new customer to the db after the new row is insert i just add the customer
to the list that bind to the grid to sea new customer added to the grid just for notice
i don't call the data from the db i just add the customer to the list

this is the code that do it :

  List<EF.GetAllCustomers_Result> lst = (List<EF.GetAllCustomers_Result>)grd.DataSource;
                        EF.GetAllCustomers_Result res = new EF.GetAllCustomers_Result();

                        res.CID = cust.CID;
                        res.Cust_Name = cust.Cust_Name;
                        res.City_Name = tav.GetCityNameByID(cust.Cust_City).ToString();
                        res.Cource_Name = tav.GetCourceName(cust.Cource_ID).ToString();
                        res.Cust_Address = cust.Cust_Address;
                        res.Cust_Phone = cust.Cust_Phone;
                        res.Rem = cust.Rem;
                        res.Media_Name = tav.GetMediaName(cust.MID);

                        lst.Add(res);
                        lst = lst.OrderBy(l => l.Cust_Name).ToList();
                        grd.DataSource = lst;


this working nice but when the SP is not : .GetAllCustomers_Result i have a problem
if the sp that fill the grid was : GET_Active_Customers i need to change everything
for every SP that i call its very bad
there is a way that i can get an instance by the grid datasource type ?
how can make it smart ?

the big problem is in the lines :
 List<EF.GetAllCustomers_Result> lst = (List<EF.GetAllCustomers_Result>)grd.DataSource;
                        EF.GetAllCustomers_Result res = new EF.GetAllCustomers_Result();
i need to get the :lst by the list that bind to the grid the same whit the res
for every sp that bind to the grid i need the same res name but in diffrent type
all the fields are the same ...

maybe i can use an interface that returns different types ?


i hope that u understand my problem ...
thanks....
0
Comment
Question by:Tech_Men
2 Comments
 
LVL 12

Accepted Solution

by:
Ammar Gaffar earned 500 total points
Comment Utility
Hi Tech_Men,

You only need to check the Type of Data Source for your grid first and then you have to write two different code (for the two different data sources)

for example:
if (grd.DataSource.GetType() == typeof(List<EF.GetAllCustomers_Result>))
    {
List<EF.GetAllCustomers_Result> lst = (List<EF.GetAllCustomers_Result>)grd.DataSource;
                        EF.GetAllCustomers_Result res = new EF.GetAllCustomers_Result();

                        res.CID = cust.CID;
                        res.Cust_Name = cust.Cust_Name;
                        res.City_Name = tav.GetCityNameByID(cust.Cust_City).ToString();
                        res.Cource_Name = tav.GetCourceName(cust.Cource_ID).ToString();
                        res.Cust_Address = cust.Cust_Address;
                        res.Cust_Phone = cust.Cust_Phone;
                        res.Rem = cust.Rem;
                        res.Media_Name = tav.GetMediaName(cust.MID);

                        lst.Add(res);
                        lst = lst.OrderBy(l => l.Cust_Name).ToList();
                        grd.DataSource = lst;
    }
        else
    {
// write here same code but for the other list
    }

Open in new window


Good Luck
0
 

Author Comment

by:Tech_Men
Comment Utility
hi ,
thanks for your answer but i found something a lot bether
i found that in entity framework i can write a SP
that returns table that becomes an list of the class objects
but i also found a trick that can turn another sp that returns the same columns
but in another where reasult   to the first type of an objects
this allows me to deal the same code for many sp its graet
in the EF u double click the second sp and chose for the combo the same name of the first sp
and its working good !!!

have fun
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now