Solved

return instance by data source type

Posted on 2014-02-16
2
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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