• Wriju's BLOG

    Channel 9 : Team Foundation Server 2010 – Creating Team Project

    • 0 Comments

    I have recently added one video in Channel 9 at http://channel9.msdn.com/Blogs/wriju/Team-Foundation-Server-2010--Creating-Team-Project 

    image

    I love the new look of Channel 9.

    Namoskar!!!

  • Wriju's BLOG

    Internet Explorer 9 Beta Guide for Developers

    • 0 Comments

    More on Developer Details at http://msdn.microsoft.com/en-us/ie/ff468705.aspx

    Namoskar!!!

  • Wriju's BLOG

    Internet Explorer 9 : The First Experience

    • 0 Comments

    While Installing…..

    image

    After it installs the IE9 the machine will be restarted and you will get a super cool browser.

    image

    IE 9 Rocks!!!

    image

    Namoskar!!!

  • Wriju's BLOG

    Internet Explorer 9 / IE 9 Download From Here

    • 0 Comments

    Internet Explorer 9 or IE9 is simply magic and launched today (Sept 15, 2010). Download it from here at http://windows.microsoft.com/en-US/internet-explorer/download/ie-9/worldwide

    Lots of canned demos are at http://ie.microsoft.com/testdrive/. I personally liked the HTML 5 power http://ie.microsoft.com/testdrive/

    Namoskar!!!

  • Wriju's BLOG

    Generate Dummy Test Data using Data Generation Plan

    • 1 Comments

    Most of the time we struggle to get some dummy data for various types of testing. Data Generation Plan in Visual Studio 2010 helps to achieve it. It not only fills the tables based on the number of rows selected by us it also takes care of the relationship if any. One thing to remember is about the quality of data, all junk.

    Let’s suppose you have a database project of Northwind. Now you need to create some 5000 categories to do a Load Testing. Very easy to achieve,

    Database Project > Add New Item > Data Generation Plan. Then choose the number of Rows.

    image

    Once done you can choose to run by hitting F5 or use menu,

    image

    Your dummy data is ready, please do not try to ready it :).

    Namoskar!!!

  • Wriju's BLOG

    Visual Studio 2010 and MSDN Licensing White Paper

    • 0 Comments

    To answer the common questions related to MSDN is available here http://www.microsoft.com/downloads/details.aspx?FamilyID=2b1504e6-0bf1-46da-be0e-85cc792c6b9d&displaylang=en

    Namoskar!!!

  • Wriju's BLOG

    Comparing Two SQL Server Schema using Visual Studio 2010

    • 0 Comments

    When you have two SQL Sever Databases and need to find the differences, Visual Studio comes really handy.

    Under Visual Studio 2010’s menu Data > Schema Compare. Once you choose New Schema Comparison

    image 

    Then you choose your databases. After you choose it you can also set the Schema Compare Settings. Once you press OK. It gives you the report.

    Let’s suppose you have one additional column in your database, it shows something,

    image

    You can further drill-down to find the details,

    image

    Now this also gives you the Script differences with highlight.

    image

    Then you can choose Write Update from menu

    image

    This will update you right selection (Target).

    Namoskar!!!

  • Wriju's BLOG

    SharePoint 2010 : Are you new?

    • 0 Comments

    Developing Applications for SharePoint 2010

    Brief Description

    This guidance provides technical information about key areas of SharePoint 2010 application development. It contains a written guide, reference implementations with source code and documentation, and a library of reusable source code components.

    http://www.microsoft.com/downloads/details.aspx?familyid=64B55569-2168-4545-8B7C-F185B2CF967D&displaylang=en

    SharePoint 2010 Walkthrough Guide

    Brief Description

    This walkthrough guide is designed to provide a step-by-step overview of Microsoft® SharePoint® 2010.

    http://www.microsoft.com/downloads/details.aspx?familyid=8C619BEF-008B-4AF2-9687-8A05848FEA97&displaylang=en

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework Complex Type

    • 0 Comments

    I have posted one blog on Complex Type and got overwhelming responses. Thanks for keeping me motivated. I also have created one Channel 9 video on Complex Types. Please visit http://channel9.msdn.com/posts/wriju/Complex-Type-ADONET-Entity-Framework/

    image 

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework POCO Template

    • 0 Comments

    Today I have published one video at Channel 9 on POCO Template. Please find them here http://channel9.msdn.com/posts/wriju/POCO-Template-in-ADONET-Entity-Framework/

    image

    Namoskar!!!

  • Wriju's BLOG

    Free Web Camps Training Kit

    • 0 Comments

    Do you want to learn Web Technologies? ASP.NET MVC 2, ASP.NET 4 Web Forms, jQuery, Entity Framework using Visual Studio 2010 with lots of Demos, Hands-On-Labs and scenario based content.

    Download and start using today from http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=0a906559-28bd-4f5d-ac18-f895a200a2a5

    Namoskar!!!

  • Wriju's BLOG

    Visual Studio 2010 Tip : Set multiple project as startup

    • 0 Comments

    This is not new in Visual Studio 2010. However this could be helpful while creating WCF based application. You may like to test your application from Visual Studio 2010 by hitting F5. By default, we can make only one project as Startup Project

    Select the solution and right click and select

    image

    image

    Namoskar!!!

  • Wriju's BLOG

    EF4 : How to enable Model without code

    • 0 Comments

    When we need to work with an Entity Framework model and use POCO. We need to get rid of the designer generated code. But how? There are few ways by which we can achieve it.

    Option 1

    I personally like this approach. You need to open the Model designer and click F4. The change the “Code Generation Strategy” to “None”.  Benefit of this approach is that this is drop down option and you can get the designer generated code back anytime later.

    image

    Option 2

    Under Model’s file property (as in pic) change the “Custom Tool” property “” from “EntityModelCodeGenerator”. If you remove it, then of required later you have to type the whole text to this property value.

    image

    Namoskar!!!

  • Wriju's BLOG

    EF4 : Complex Type

    • 0 Comments

    Is to simplify your life which might seem too complex :). Let’s suppose you have a table like,

    image

    Now you might want to club all address related field and make one single representation to it. Once you have created the model in your Visual Studio 2010, you may just need to select all the properties and choose the option to create the Complex Type.

    image

    Once you have done it, the Model Browser and Model would look like,

    image

    Your query to fetch the required data then would look like,

    using (var ctx = new TestDBEntities())
    {
        var q = from c in ctx.Addresses
                select c;

        foreach (var k in q)
        {
            Console.WriteLine(k.AddLines.Street);
            Console.WriteLine(k.AddLines.Country);
        }
    }

    Namoskar!!!

  • Wriju's BLOG

    SQL Server 2008 R2 Management Studio designer was not allowing me to add primary key to a column of an empty table

    • 0 Comments

    What the heck!!! Then finally Amar did help me to get it done. Simple “option”. Life has too many “options”.

    Tools > Options

    image

    You need to just uncheck the option “Prevent saving changes that require table re-creation”

    Namoskar!!!

  • Wriju's BLOG

    Every Developer needs to be Social

    • 0 Comments

    This is exactly Scott Hanselman communicated on which we live our everyday life. How were those days when there were only books and different CDs of MSDN. Then Google and Bing came into the picture. Being a developer how we drink resource and make them useful for others.

    image

    http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-Social-Networking-for-Developers-Part-1-Every-Developer-Needs-a-Blog/

    Every developer should watch it !!!

    Namoskar!!!

  • Wriju's BLOG

    EF4: What’s in it there in Foreign Key

    • 0 Comments

    There are a lot of buzz around the Foreign Key support in ADO.NET Entity Framework v4. Question often comes to our mind that what is so special here? While discussing with my fellow Consultant Vijay, he gave me a nice example. Let me share here,

    Let’s assume that you have two tables Dept and EmpDept. Connected with DeptId which is Primary in Dept table and Foreign key in EmpDept table. When we simply use SQL query we can easily change the DeptId of an employee by sending one single Update statement like

    UPDATE EmpDept SET DeptId = 2 WHERE EmpId = 1

    This simple thing was totally missing in the previous version of EF (in Visual Studio 2008). We had to pull out the whole new Dept to update an existing EmpDept.

    If we have the database table like,

    image

    From it if we have built the model like, (as it was in Visual Studio 2008)

    image

    Notice, here you do not have the DeptId as static property in the EmpDept entity. Thus the pain. So to simply shift one employee from one dept to another you had to write something like,

    When no Property for FK
    1. using (var ctx = new TestDBEntities())
    2. {
    3.     //Get the Employee to Update
    4.     EmpDept q = (from e in ctx.EmpDepts where e.EmpId == 2 select e).First();
    5.  
    6.     //Get the Dept to update to
    7.     Dept dd = (from d in ctx.Depts where d.DeptId == 3 select d).First();
    8.  
    9.     //Finally Update
    10.     q.Dept = dd;
    11.  
    12.     ctx.SaveChanges();
    13. }

    Which would execute 3 queries in the database.

    T-SQL
    1. --Query 1 : Get Emp to Update
    2. SELECT TOP (1) [Extent1].[EmpId] AS [EmpId], [Extent1].[EmpName] AS [EmpName], [Extent1].[DeptId] AS [DeptId]
    3. FROM [dbo].[EmpDept] AS [Extent1] WHERE 2 = [Extent1].[EmpId]
    4.  
    5. --Query 2 : Get the Dept
    6. SELECT TOP (1) [Extent1].[DeptId] AS [DeptId], [Extent1].[DeptName] AS [DeptName]
    7. FROM [dbo].[Dept] AS [Extent1] WHERE 3 = [Extent1].[DeptId]
    8.  
    9. --Query 3 : Finally Update it
    10. exec sp_executesql N'update [dbo].[EmpDept] set [DeptId] = @0 where (([EmpId] = @1) and ([DeptId] = @2))
    11. ',N'@0 int,@1 int,@2 int',@0=3,@1=2,@2=2

    Now, in EF4 you get the foreign key as static column. So things becomes much more simpler both from the front end code perspective and backend execution. The new model looks like as below if we select the “Include foreign key columns in the model

    image

    as,

    image

    Now notice here the new static property in EmpDept as DeptId. If you want to achieve the same goal here, things would become much more simpler,

    When FK is static property
    1. using (var ctx = new TestDBEntities())
    2. {
    3.     EmpDept q = (from e in ctx.EmpDepts where e.EmpId == 2 select e).First();
    4.  
    5.     q.DeptId = 2;
    6.  
    7.     ctx.SaveChanges();
    8. }

    And also the generated SQL would be only 2,

    T-SQL
    1. --Query 1 : Get Emp to Update
    2. SELECT TOP (1) [Extent1].[EmpId] AS [EmpId], [Extent1].[EmpName] AS [EmpName], [Extent1].[DeptId] AS [DeptId]
    3. FROM [dbo].[EmpDept] AS [Extent1] WHERE 2 = [Extent1].[EmpId]
    4.  
    5. --Query 2 : Finally Update it
    6. exec sp_executesql N'update [dbo].[EmpDept] set [DeptId] = @0 where (([EmpId] = @1) and ([DeptId] = @2))
    7. ',N'@0 int,@1 int,@2 int',@0=3,@1=2,@2=2

    I personally still feel that being ORM we are paying the additional database roundtrips here. And if you want to reduce it again to simple one either you use Stored Procedure and create Function or execute RAW T-SQL using ExecuteStoreQuery method of Context.

    Namoskar!!!

  • Wriju's BLOG

    EF4 : Use MergeOption.NoTracking for better query performance

    • 0 Comments

    If you are just trying to fetch the data and has no intention to update it. Then you should be using the MergeOption.NoTracking to ensure that the ObjectStateManager does not store the required information for update and delete. Hence your query would be more quick in nature.

    using (NorthwindEntities ctx = new NorthwindEntities())
    {
        ctx.Customers.MergeOption = MergeOption.NoTracking;

    So this simple line would add a lot of value to your performance and clearly visible in second call onwards.

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework 4.0: Making it fast through Compiled Query

    • 1 Comments

    If we are using similar query frequently, we can increase it’s performance by Compiling it through CompiledQuery. It’s always recommended to use CompiledQuery if you happen to see the query is getting executed many times.

    Let’s take an example, in Northwind database if you are getting Customer based on City. You may follow the below approach. Also a very important point to observe is to see how it runs faster in subsequent calls even though the parameter values differ.

    Compiled Query
    1. private static readonly Func<NorthwindEntities, string, IQueryable<Customer>> myCompiledQuery =
    2.     CompiledQuery.Compile<NorthwindEntities, string, IQueryable<Customer>>(
    3.     (ctx, city) => from c in ctx.Customers
    4.                     where c.City == city
    5.                     select c);

    Now, instead of writing raw LINQ use this Compiled Query

    Calling it
    1. private static void CallCompiledQuery(string myCity)
    2. {
    3.     using (NorthwindEntities ctx = new NorthwindEntities())
    4.     {
    5.         var q = myCompiledQuery(ctx, myCity);
    6.  
    7.         foreach (var k in q)
    8.         {
    9.             Console.WriteLine(k.CompanyName);
    10.         }
    11.     }
    12. }

    We also wanted to capture the time.

    1. static void CallIt(string sCity)
    2. {
    3.     Stopwatch sw = new Stopwatch();
    4.     sw.Start();
    5.  
    6.     CallCompiledQuery(sCity);
    7.  
    8.     Console.WriteLine("+++++++++++++++++++++");
    9.     Console.WriteLine("Elapsed Time in Milliseconds : {0}", sw.ElapsedMilliseconds);
    10.     sw.Stop();
    11. }

    Just call twice to capture the time,

    1. static void Main(string[] args)
    2. {
    3.     Console.WriteLine();
    4.     Console.WriteLine("++++++++++++++++");
    5.     Console.WriteLine("Call No. 1");
    6.     Console.WriteLine("++++++++++++++++");
    7.     CallIt("London");
    8.     Thread.Sleep(2000);
    9.     Console.WriteLine();
    10.  
    11.     //
    12.     Console.WriteLine();
    13.     Console.WriteLine("++++++++++++++++");
    14.     Console.WriteLine("Call No. 2");
    15.     Console.WriteLine("++++++++++++++++");
    16.     CallIt("México D.F.");
    17.     Thread.Sleep(2000);
    18.     Console.WriteLine();
    19. }

    In my machine the first call took 322 milliseconds and the second one took 4 milliseconds. This may differ time to time and machine to machine. But this is faster indeed.

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework 4.0: Simple N-Tier with Self-Tracking Entities and WCF

    • 1 Comments

    ADO.NET Entity Framework 4.0 allows us to easily create N-Tier Application with the help of Self-Tracking Entities. This means now we do not have to worry about merging and checking values in more disconnected scenario. Here is how we can do it in very simple example to start with.

    Create Model Layer

    ·         Create a blank solution in Visual Studio 2010 with the name NTier.

    ·         Create a new Class Library application. Give the name TestDBModel.

    ·         Right click to the TestDBModel project and choose Add New Item. Add ADO.NET Entity Data Model and name it “TestDBModel.edmx”.

    ·         Point to the database and table (here it is Emp)

    ·        The Emp table is simple and has

     

    CREATE TABLE [dbo].[Emp](

         [EmpId] [int] IDENTITY(1,1) NOT NULL,

         [EmpName] [varchar](50) NULL,

     CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED

    (

         [EmpId] ASC

    ) 

    ·        Then select the TestDBModel.edmx from the designer and go to the property window and select the property “Code Generation Strategy” and change it to None. The reason behind doing it is to get WCF enabled entity classes which can keep the track of changes made across the layers without having an open connection (context).

    ·         Then right click on the TestDBModel.edmx designer and select “Add Code Generation Item”.

    ·         Choose “ADO.NET Self-Tracking Entity Generator” and name it Model1.tt.

    ·        If you explore it you will find the context and Emp class created by that template already. You do not have to write/change anything there.

     

    Create Service Layer

    ·        Now create a WCF Class Library project with a name WcfServiceLibrary1.

    ·     Add a class file ITestDB.cs with the following code.

    namespace WcfServiceLibrary1
    {
        [ServiceContract]
        public interface ITestDB
        {
            [OperationContract]
            Emp[] GetEmp();

            [OperationContract]
            Emp[] UpdateEmployee(Emp[] emps);        
        }
    }

    ·     Now add one more class file TestDBService.cs with the following code,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using TestDBModel;

    namespace WcfServiceLibrary1
    {
        public class TestDBService : ITestDB
        {
            public Emp[] GetEmp()
            {
                TestDBEntities ctx = new TestDBEntities();
                return ctx.Emps.ToArray();
            }

            public Emp[] UpdateEmployee(Emp[] emps)
            {
                throw new NotImplementedException();
            }
            
        }
    }

    ·         You also need to bring the EF connection string from the model project to this project’s App.config file.

     

    Create UI Layer

    ·         Create a Windows Forms Application with a name WindowsFormsApplication1. Add Service reference to your WCF Class Library.

    ·        Then add Grid View named dataGridView1 and Button named button1.

    ·        Add the following code,

    private void button1_Click(object sender, EventArgs e)
    {
        var svc = new ServiceReference1.TestDBClient();

        dataGridView1.DataSource = svc.GetEmp();
    }

    I also have added the code here (check below of this post if you have clicked the post title to read it)

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework 4.0 : Case Studies

    • 0 Comments

    Check out who all using at http://msdn.microsoft.com/en-us/data/ff625830.aspx 

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework 4.0 : Model First Channel 9

    • 0 Comments

    I have published one video at Channel 9 on Model First approach.

    image

    http://channel9.msdn.com/posts/wriju/Model-First-in-ADONET-Entity-Framework-4/

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework 4.0 : Loading Data in 4 ways

    • 1 Comments

     

    We are working with Relational Database and using ADO.NET Entity Framework 4.0, it is inevitable that we need to load data. EF4 has improved Lazy Loading feature as compared to its previous version. We can also combine both Lazy and Eager loading.

     

    Based on the amount of data we bring in and type of application we develop, we need to decide which one to choose. There is no single solution.

     

    I have used two tables from Northwind database – Customer and Order (1 to many).

    image

    Now, each Customer will have n number of orders, so ideal for Lazy or Immediate loading.

     

    Where is the catch?

     

    This uses a feature introduced in ADO.NET 2.0 known as MultipleActiveResultSets (MARS). This gets added to the connection string of EF4.

     

    Scenario 1: When Lazy loading is enabled (default)

     

    By default in EF4 Lazy Loading is enabled through the Model designer. Which means if you write the below code, to get for every Order, it would send a SQL statement to the SQL Server.

     

    using (var ctx = new NorthwindEntities())

    {

        var q = from c in ctx.Customers

                select c;

     

        foreach (var cust in q)

        {

            Console.WriteLine("Customer : {0}", cust.CompanyName);

            Console.WriteLine("Respective Orders...");

     

            foreach (var ord in cust.Orders)

            {

                Console.WriteLine("Order given on {0}", ord.OrderDate);

            }

            Console.WriteLine();

            Console.ReadKey();

        }

    }

     

    In the background for the first foreach loop it would execute the query in SQL Server to fetch the data from only Customers table. Then for the inner foreach loop it would run individual query to get respective set of orders for each customer. So if there are 100 customers and 1000 orders, the total number of queries would be 1001.

     

    Pros and Cons

    This is acceptable in situations like where you have paged navigation and only when user asks for it, you load it, else leave it. This involves multiple round trips.

    But when you want to pass data thorough layers, you would not be using it as because there will be no active open context. So when it would try to navigate to the child information no data will be found.

     

     

    Scenario 2: Explicit Loading or conditional Lazy Loading

     

    If you want to control the lazy loading on your own by loading the data only if logic requires, you should be disabling the Lazy Loading option and explicitly call it.

     

    In the code, you need to add the line ctx.ContextOptions.LazyLoadingEnabled = false; This will switch off the Lazy Loading.  

     

    //Lazy loading OFF

    ctx.ContextOptions.LazyLoadingEnabled = false;

    var q = from c in ctx.Customers

            select c;

     

    foreach (var cust in q)

    {

        Console.WriteLine("Customer : {0}", cust.CompanyName);

        Console.WriteLine("Respective Orders...");

     

        //Conditionally Load the child information

        if (1 == 1)

            cust.Orders.Load();

     

        foreach (var ord in cust.Orders)

        {

            Console.WriteLine("Order given on {0}", ord.OrderDate);

        }

        Console.WriteLine();

        Console.ReadKey();

    }

     

    Pros and Cons

    This helps us to control the number of roundtrips based on application need. However, we need to be extra careful about testing our code before final deployment.

     

     

    Scenario 3: When Eager Loading is enabled

     

    If we switch off the Lazy Loading as discussed in Scenario 2 and want all the records to be available in the memory, then we need to use .Include(Entity).

     

    //Lazy loading OFF

    ctx.ContextOptions.LazyLoadingEnabled = false;

     

    var q = from c in ctx.Customers.Include("Orders")

            select c;

     

    foreach (var cust in q)

    {

        Console.WriteLine("Customer : {0}", cust.CompanyName);

        Console.WriteLine("Respective Orders...");

     

        foreach (var ord in cust.Orders)

        {

            Console.WriteLine("Order given on {0}", ord.OrderDate);

        }

        Console.WriteLine();

        Console.ReadKey();

    }

     

    Pros and Cons

    This brings all the Customer and Order information via a single query. This is required when we pass data from one layer to another layer. It also reduces the number of roundtrips. However, it can capture large about of your memory space.

     

    If we check in the profiler we would see a single query like (removed few characters)

     

    SELECT

    [Project1].[C1] AS [C1],

    [Project1].[CustomerID] AS [CustomerID],...

    FROM ( SELECT

          [Extent1].[CustomerID] AS [CustomerID], ...

          1 AS [C1],

          [Extent2].[OrderID] AS [OrderID], ...

          CASE WHEN ([Extent2].[OrderID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]

          FROM  [dbo].[Customers] AS [Extent1]

          LEFT OUTER JOIN [dbo].[Orders] AS [Extent2] ON [Extent1].[CustomerID] = [Extent2].[CustomerID]

    )  AS [Project1]

    ORDER BY [Project1].[CustomerID] ASC, [Project1].[C2] ASC

     

     

    Scenario 4: Combination of Eager and Lazy Loading

     

    We could also use .Include and enable lazy loading if we wish to load few data lazily and some one-shot. There is no harm doing it as we are taking the benefit of both the features.

     

    MSDN References

     

    Loading Related Objects (Entity Framework)

    http://msdn.microsoft.com/en-us/library/bb896272(VS.100).aspx

     

    I would like to present this blog post to my sweet daughter Wrishika and lovely wife Saswati – without them I would have never had so much of energy to continue.

     

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework 4.0 : DataBinding with Windows Presentation Foundation (WPF)

    • 0 Comments

    This post is to give an idea on how easily we can bind and create master details display in WPF.

     

    Here I am using Northwind sample database and using Category and Products tables. These two tables are connected with each other through CategoryId. We will create a model using these two tables. Please ensure that you check the Foreign Key checkbox while selecting table in the EF4 model creation wizard.

    image

    Now, add two controls ListBox with id listBox1 and DataGrid with dataGrid1. Set the AutoGeneratedColumns to checked for the DataGrid.

     

    Now open the Show Data Source from Visual Studio from Data Source menu.

     

    Drop Category to listBox1 and Product (under Category) to dataGrid1. That’s all. Not a single line of code is required.

     

     

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework 4.0 : DataBinding with Windows Forms

    • 1 Comments

    This post is to give an idea on how easily we can bind and create master details display in Windows Forms Application.

     

    Here I am using Northwind sample database and using Category and Products tables. These two tables are connected with each other through CategoryId. We will create a model using these two tables. Please ensure that you check the Foreign Key checkbox while selecting table in the EF4 model creation wizard.

    image

    Now, in the Forms1 add two controls. Combo Box with id comboBox1 and Data Grid View with id dataGridView1.

     

    Add the below code

    private void Form1_Load(object sender, EventArgs e)

    {

        using (var ctx = new NorthwindEntities())

        {

            comboBox1.DataSource = ctx.Categories.ToList();

            comboBox1.ValueMember = "CategoryID";

            comboBox1.DisplayMember = "CategoryName";

        }

    }

     

    And

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

    {

        int iCatID = 0;

     

        try

        {

            iCatID = (int)comboBox1.SelectedValue;

        }

        catch { }

     

        if(iCatID > 0)

        {

            using (var ctx = new NorthwindEntities())

            {

                var q = from p in ctx.Products

                        where p.CategoryID == iCatID

                        select p;

     

                dataGridView1.DataSource = q.ToList();

            }

        }

    }

     

    Namoskar!!!

Page 8 of 22 (548 items) «678910»