Translate

jueves, 27 de febrero de 2014

ASP.NET: Populating DropdownList with data from two tables using Entity Framework

This time we are going to populate a DropdownList with data from two differents tables. The tables were implemented using  Entity Framework code first. Those are the tables from which we are going to extract the data. We need the Id and the Name from the RaceTypes table, and the name from the Sports Tables.


This is the DropdownList showing the Name from the Sports, and the Name from the RaceTypes. Once you select a value from the DropDownList it would return the correspondet Id from the RaceTypes.
 

This is the code for the DropDownList. We use the field SelectMethod to indicate wich method are we going to use to populate data, DataTextField to indicate which data are we going to show, and DataValueField to indicate wich value return.




And this is the code behind with the method used to retrieve data.





viernes, 21 de febrero de 2014

WPF Grid Rounded Corners, shadows, and button gradient effect


A few effect examples in Windows Presentation Foundation Applications:

  • Grid with Rounded corners and a shadow effect.
  • Data Grid with rounded corners.
  • Buttons with a gradient color.



This is the code for the XAML.



martes, 4 de febrero de 2014

DropDownList: Select an Item using code

Suppose we have two fields, the first one a Text Box showing an ID and a the second one a DropDownList with the name associated to this ID.



If you want the dropdownlist to change its value when the Text Box change his value also, you could do it in an programatically way 

             ListItem aValue = ddlDeportes.Items.FindByValue(txbxDeporte.Text);
            ddlDeportes.SelectedIndex = ddlDeportes.Items.IndexOf(aValue);



It´s important to configure the dropdownlistconnecting the value Field to the ID, and the text field to the name which is showed by the dropdownlist.
In the first line we search into the dropdownlist using the value showed in the text box. Then we obtain a ListItem object containing the object to be selected into the dropdownlist. Finally using IndexOf we select this item in the dropdownlist

On the other way, if you want to change the value in the TextBox when you select a value from the dropdownlist then, you have to use the event OnSelectedIndexChanged for the dropdownlist, and use the following code.

          txbxDeporte.Text = Convert.ToString(ddlDeportes.SelectedValue); 

Is important also to enable the AutoPostBack property for the dropdownlist. Then the mark-up code for this component would be as follows:



 
ddlDeportes_GetData would be the method used to populate the dropdownlist using LINQ to get the data:

public IQueryable ddlDeportes_GetData()
        {
            var db = new ClubSiteContext();
            IQueryable query = from sports in db.Sports
                                      orderby sports.Name
                                      select sports;
            return query;
        }


sábado, 1 de febrero de 2014

How to seed data in a "manual mode" way into an Entity Framework Code First data base

First of all, you need to enable automatic migrations: go to Tools, Library Package Manager and finally to Package Manager Console.

Execute the command: Enable-Migrations. A new class called Configuration appears under a new folder called Migrations. Go to this class and use the following code as an example to seed two tables called Sport and RaceType:




The first method, Seed which is overrided to use our own seed actions includes a call to an SQL Command, in order to delete the tables, and initialize the index counters. Following to a call to the method  GetSports, which obtains data from the Sports List created bellow. For each element on this list we call the Add method.

To manually launch this seed you only need to use the command Update-Database in the previous shell.