jueves, 26 de diciembre de 2013

LINQ Examples


 Example 1

listProjectPersonDO  is a List<ClsRelProjectPersonDO>

foreach (ClsRelProjectPersonDO clsRelProjectPersonDO in 
            listProjectPersonDO.Where(element => element.IdProjectDomainSubdomain == row))
            {


             }


 Example2

 GeneralReport is a DataTable

 var qWorkingArea = GeneralReport.AsEnumerable()
             .GroupBy(r => r.Field<String>("workingArea"))
            .Select(g => new
            {
                totalW = g.Count(),
                workingArea = g.Key,

                onBoardW = ((from c in g where c["status"].ToString() == "ON BOARD" select c).Count())

              })
            .OrderBy(x => x.workingArea);


varGetTotalWorkingAreasOB = qWorkingArea.Sum(x => x.onBoardW); 


Example 3

How to do many selects.
 
var result = pyramid.Select(element => element.ClsRelProjectPersonDO.Where(item => item.IdProjectPerson == 
idProjectPerson)).First();

IEnumerable<ClsRelProjectPersonDO> a = result;

martes, 12 de noviembre de 2013

Asp - Send a popup message

 To send a message follow the next steps:

  1. Add the reference:  using AjaxControlToolkit; 
  2. Build the message through an alert using JQuery
  3. Use the method RegisterClientScriptBlock 

 The function should look like this:

        StringBuilder sb = new StringBuilder();
        sb.Append("<script type=\"text/javascript\">alert('");
        sb.Append(mensaje.Replace("'", ""));
        sb.Append("'); ");
        sb.Append("window.location.href='");
        sb.Append(url);
        sb.Append("';</script>");
       ToolkitScriptManager.RegisterClientScriptBlock(this, typeof(Button), "Mensaje", sb.ToString(), false);

jueves, 7 de noviembre de 2013

SQL - User-Defined Type (DataTable)

How to create a User-Defined in your database and use it in C# like DataTable

Create the data type and refresh the DB

[type_name] - The name for the new data type
[parameter] - The name of your paramenter
int - The data type
NULL - Declare if the parameter has to be null or not

 CREATE TYPE [relProjectDS] AS TABLE(
 

    [idProject]
int NOT NULL ,
    [idCatDomain] int NULL,
    [catSubdomain] [nvarchar]  (50) NULL

)

Create the Store procedure

CREATE PROCEDURE  [dbo].[insertProject]
    @myDataType relProjectDS readonly
AS
BEGIN
      insert into [dbo].[Table] select * from @myDataType
END

Create and fill the DataTable in C#

idProject- Name of the parameter according with data type from the DB
typeof(int) - The type definition according with the DB
Rows.Add(param1, param2) - The method to add each row to the DataTable

DataTable projectsTable = new DataTable();
projectsTable.Columns.Add("
idProject", typeof(int));
projectsTable.Columns.Add("idCatDomain", typeof(int));
projectsTable.Columns.Add("idCatSubdomain", typeof(int));


projectsTable.Rows.Add(1,2,3,4,5);



Pass dataTable as parameter

using (var command = new SqlCommand("InsertTable") {CommandType = CommandType.StoredProcedure})
{
    var dt = new DataTable(); //create your own data table
    command.Parameters.Add(new SqlParameter("@myDataType", dt));
    SqlHelper.Exec(command);
}

Update dataTable in Store Procedure

In the store procedure our dataType is readonly so to modify it we can pass to a temporally table and then update the data.

In the following steps after insert in another table we get the ID and the update the ID in the temporally table.

Finally we insert the teporally table in the DB. 
  1.  SET @idProject = (SELECT SCOPE_IDENTITY()) 
  2.  INSERT INTO #temp_table SELECT * FROM @relProjectDS
  3. UPDATE #temp_table SET idProject = @idProject 
  4. INSERT INTO [dbo].[relProjectDomainSubdomain] (idProject, idCatDomain, idCatSubdomain) SELECT idProject, idCatDomain, idCatSubdomain FROM  #temp_table

 

Tips:

How to create a temporal table

create table #temp_table
    (
        [idProjectDomainSubdomain] [int] IDENTITY(1,1) NOT NULL,
        [idProject] [int] NULL,
        [idCatDomain] [int] NOT NULL,
        [idCatSubdomain] [int] NULL,
    )










jueves, 31 de octubre de 2013

ASP - Inside a Gridview how get the DropDownList index

In the view:


  • Define the method in the event OnSelectedIndexChanged
  • Add the tag AutoPostBack="true"


<asp:TemplateField HeaderText="Competence Domain">
       <ItemTemplate>
              <asp:DropDownList ID="ddlCompDomain" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="ddlCompDomain_SelectedIndexChanged">
              </asp:DropDownList>
       </ItemTemplate>
</asp:TemplateField>


In the code inside the method ddlCompDomain_SelectedIndexChanged:


  1. Cast the sender object to the element you wanted to know the index, for our case a DropDownList
  2. From the DropDownList get the GridViewRow through the parent property
  3. Get the index with RowIndex


protected void ddlCompDomain_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlCurrentDropDownList = (DropDownList)sender;
        GridViewRow grdrDropDownRow = ((GridViewRow)ddlCurrentDropDownList.Parent.Parent);

        TextBox2.Text = grdrDropDownRow.RowIndex.ToString();

        TextBox3.Text = ddlCurrentDropDownList.SelectedItem.Text;
    }

martes, 29 de octubre de 2013

JQuery - Ajax

AJAX (Asynchronous JavaScript Xml)


This is the method to invoke ajax

$.ajax({   
     url: "my_data.xml" ,      // URL of you want to GET via AJAX
    cache: false,
    dataType: "xml",           // data - The data return from the Ajax call
    success: function(xml_reader){       // Run this function is the method ajax is successful
        $(xml).find("runner").each(function() {

  var info = '<li>Name: ' + $(this).find("fname").text() + ' ' + $(this).find("lname").text() + '</li>';

 if( $(this).find("gender").text() == "m" ){
$('#finishers_m').append( info );
}else if ( $(this).find("gender").text() == "f" ){
$('#finishers_f').append( info );
}
  $('#finishers_all').append( info );
});                
    }

});


Ajax methods in JQuery

$.get()
$.getJSON(url_to_load, function(json){} ) - This method is the shortcut from $.ajax();
$.getScript()
$.post()
$.load()

Send information using Ajax

There are two ways: serialize and serializeArray

$("#my_form").serialize(); -  Sent the data separeted by &
Ej.-  a=1&b=3&c=5

$("#my_form").serializeArray();  -  Sent the data as following
{
   {
       a: "1", 
       b: "3",
       c: "5"  
   },
  {
       a: "11" ,
       b: "31",
       c: "51"  
   },
}

This method sent the information to the defined URL
$.post(url_to_send , data , function(json){ 
});








sábado, 26 de octubre de 2013

JQuery - Part II

Declare Objects

You can create objects using JavaScript, also you can assign this object to a variable.

function object(param1, param2){
 this.param   = param1;
 this.param2= param2;

  my_function: function(){
  
  }
}

var my_object = object(1, 2);

Properties

To access the properties use object.property

Ej:  var name =  object.param2;

Using JSON the syntax is    object['param2']

Arrays

The arrays can be created with the following sintax

var array = new Array();
var array = new Array('obj1', 'obj2', 'obj3');
var array = ['obj1', 'obj2', 'obj3'];

Functions

$.inArray get the index where is the element.
var element_index = $.inArray('value', array);


Window object

Each time a tab is open a window object is created.

Methods

window.onFocus - tells when a tab is activated.

window.onBlur - detects when the tab lose the focus.

Functions

window.setTimeout( function(){}, 400) - Wait the defined time before call the function

window.setInterval( function(){}, 400) - interval between a function is run repeatedly

window.clearInterval( function(){} ) - wipes clear the schedule of repetead function calls