Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

return instance by data source type

Posted on 2014-02-16
2
Medium Priority
?
302 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 1500 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

705 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