Solved

return instance by data source type

Posted on 2014-02-16
2
290 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
ID: 39864695
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
ID: 39865677
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

11 Experts available now in Live!

Get 1:1 Help Now