Solved

return instance by data source type

Posted on 2014-02-16
2
294 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Save json data from URL using SSIS 1 23
itextsharp with c# 3 18
Error building VS2105 solution from repository 1 33
Linked Server - SP with Param to VIew 7 21
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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