Saturday, February 18, 2012

Event c#

using System;

public delegate void DivBySevenHandler(object o, DivBySevenEventArgs e);

public class DivBySevenEventArgs : EventArgs
{
    public readonly int TheNumber;
   
    public DivBySevenEventArgs(int num)
    {
        TheNumber = num;
    }   
   
}

public class DivBySevenListener
{
    public void ShowOnScreen(object o, DivBySevenEventArgs e)
    {
        Console.WriteLine(
            "divisible by seven event raised!!! the guilty party is {0}",
            e.TheNumber);
    }   
}

public class BusterBoy
{
    public static event DivBySevenHandler EventSeven;
   
    public static void Main()
    {
        DivBySevenListener dbsl = new DivBySevenListener();
        EventSeven += new DivBySevenHandler(dbsl.ShowOnScreen);
        GenNumbers();
    }
   
    public static void OnEventSeven(DivBySevenEventArgs e)
    {
        if(EventSeven!=null)
            EventSeven(new object(),e);
    }   
   
    public static void GenNumbers()
    {
        for(int i=0;i<99;i++)
        {
            if(i%7==0)
            {
                DivBySevenEventArgs e1 = new DivBySevenEventArgs(i);
                OnEventSeven(e1);
            }
        }       
    }
       
}
-------------------------------------------------------------------------
  1. Event handler is something defined in a class, like the sample you gave me. You can think of an Event handler as a type, like a class or a primitive type.
  2. Class Skiepage
    {
           …
    public
    …     
    EventHandler<EventArgs> SelectedObjectChanged;Skiepage skiepage = new Skiepage();SelectedObjectChanged += EventConsumer_selectChanged;private void EventConsumer_selectChanged(object o, EventArgs e)
}
When you consume an event handler, you usually register a method to that event handler
Class EventComumer
{
      
       skiepage.
             
              {
                     Do something;
              }
}
Notice that how an EventHandler(SelectedObjectChanged) is defined in a class and how to attach a method to an event handler

Friday, February 17, 2012

Linq

IDictionary<int, String> d = result.ToDictionary(sp_getAllRRPFirmType=>sp_getAllRRPFirmType.RRPFirmTypeCode, sp_getAllRRPFirmType=>sp_getAllRRPFirmType.Description);


result.Select(sp_getAllRRPFirmType=>sp_getAllRRPFirmType.RRPFirmTypeCode)

result.toList(sp_getAllRRPFirmTypeResult)

where sp_getAllRRPFirmType is the function name and RRPFirmTypeCode is the return value from the function
result is a ISingleResult

Wednesday, February 15, 2012

sorting in gridview

if datasourcecontrol is not used, you have to handle sorting and paging by yourself in on_paging or on_sorting
public string GetSortDirection(string SortExpression)
 {
    if (ViewState[SortExpression] == null)
        ViewState[SortExpression] = "Desc";
    else
        ViewState[SortExpression] = ViewState[SortExpression].ToString() == "Desc" ? "Asc" : "Desc";

     return ViewState[SortExpression].ToString();
  }

  protected void GridView_OnSorting(object sender, GridViewSortEventArgs e)
 {
    string order = GetSortDirection(e.SortExpression);
    string s = "select * from [User] order by " + e.SortExpression + " " + order;
    BindGrid(s);
 }

 void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ((GridView)sender).PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

Wednesday, February 8, 2012

issues: when upgrading ssrs2005 to 2008, get this error--The processing of Parent for the tablix cannot be performed. Cannot compare data of types System.String and System.Int16. Please check the data type returned by the Parent. 

Here is the solution:

It seems the report was originally designed in RS 2005. The main part contains a list with a nested table inside. The table has 8 table groups and 1 detail group.

Now based on the dataset, it seems this is just a regular nested grouping of data - however in the report *every* table group has an explicitly defined parent group expression. The parent group expression always seems to match the containing group's group expression.

In RS 2005, this (probably unintentional and) errorness recursive grouping was ignored. However in RS 2008 that's not the case and the report fails with the following internal error message:

The processing of Parent for the tablix ‘tblTimesheet’ cannot be performed. Cannot compare data of types System.Decimal and System.String. Please check the data type returned by the Parent.

Note that it tries to compare a String and a Decimal - this happens because the grpEmployee group expression is a string, but the grpRate group expression is a Decimal, and on the grpEmployee group the rate was specified as parent group expression.

The solution for the reports is to remove all those recursive parent group expressions, because you really just want regular nested table groups, but not a recursive hierarchy.
Again, if you just want nested groups, please do NOT specify a recursive parent group expression. Those should only be used if you really have recursive hierarchies in your data (e.g. parent-child hierarchy in a SSAS dataset).

I attached both modified reports (compare them in Windiff with your original reports and you will see that I just removed all the parent group expressions).

"cangrow " of a textbox's default value is false  in 2005, need to set true in 2008r2

Monday, February 6, 2012

How to create dbml and invoke stored procedure with LINQ


LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures)

Over the last few weeks I've been writing a series of blog posts that cover LINQ to SQL.  LINQ to SQL is a built-in O/RM (object relational mapper) that ships in the .NET Framework 3.5 release, and which enables you to model relational databases using .NET classes.  You can use LINQ expressions to query the database with them, as well as update/insert/delete data.
Below are the first five parts of my LINQ to SQL series:
In these previous LINQ to SQL blog posts I demonstrated how you could use LINQ query expressions to programmatically retrieve data from a database.
In today's blog post I'll cover how you can also use database stored procedures (SPROCs) and user defined functions (UDFs) with your LINQ to SQL data model.  Today's blog post will specifically cover how to call SPROCs to query and retrieve data from the database.  In my next blog post in this series I'll then show how you can optionally also use SPROCs to update/insert/delete data from the database.

To SPROC or not to SPROC?  That is the question....

The question of whether to use Dynamic SQL generated by an ORM or instead use Stored Procedures when building a data layer is a topic that generates endless (very passionate) debate amongst developers, architects and DBAs.  A lot of people much smarter than me have written on this topic, so I won't rehash the arguments for and against each side here.
The LINQ to SQL ORM that ships in .NET 3.5 is pretty flexible, and can be used to create data model classes whose object model can be independent of the underlying database schema, and which can encapsulate business logic and validation rules that work regardless of whether the data model is populated/persisted via dynamic SQL or via SPROCs.
In my LINQ to SQL Part 3: Querying our Database post I discussed how you can write LINQ query expressions against a LINQ to SQL data model using code like below:

When you write LINQ query expressions like this the LINQ to SQL ORM will execute the necessary dynamic SQL for you to retrieve Product objects that matches your query.
As you'll learn in this post, you can also optionally map SPROCs in the database to your LINQ to SQL DataContext class, which allows you to alternatively retrieve the same Product objects by calling a stored procedure instead:
 
This ability to use both dynamic SQL and SPROCs with a clean data model layer is pretty powerful, and provides a great deal of flexibility when working on projects.

The Steps to Map and Call a SPROC using LINQ to SQL

In my Part 2: Defining our Data Model Classes tutorial I discussed how to use the LINQ to SQL ORM designer to create a LINQ to SQL class model like below:

Notice above how there are two panes on the LINQ to SQL ORM designer surface.  The left pane enables us to define data model classes that map to our database.  The right method pane allows us to optionally map SPROCs (and UDFs) to our LINQ to SQL DataContext object, which we can then use in-place of dynamic SQL to populate the data model objects.
How to Map a SPROC to a LINQ to SQL DataContext
To map SPROCs to our DataContext class, let's first go to the VS 2008 Server Explorer window and look at the SPROCs within our database:

We can double click any of the SPROCs above to open and edit them.  For example, below is the "CustOrderHist" SPROC in Northwind:

To map the above SPROC to our LINQ to SQL DataContext, we can drag/drop it from the Server Explorer onto our LINQ to SQL ORM designer.  This will automatically create a new method on our LINQ to SQL DataContext class like below:

By default the method name created on the DataContext class will be the same as the SPROC name, and the return type of the method will be an automatically created type that follows the "[SprocName]Result" naming pattern. For example: the SPROC above would return a sequence of "CustOrderHistResult" objects.  We could optionally change the name of the method by selecting it in the designer and then use the property grid to rename it.
How to Call our Newly Mapped SPROC
Once we've done the steps above to map a SPROC onto our DataContext class, it is easy to use it to programmatically retrieve data.  All we need to-do is call the new method we mapped on our DataContext class to get back a sequence of strongly typed results from the SPROC:
Calling the SPROC in VB:

Calling the Sproc in C#:

In addition to programming looping over the result like in the code samples above, I could also obviously bind the results to any UI control to display them.  For example, the below code databinds the result of our SPROC to a <asp:gridview> control:

Which then displays the product history of our customer on a page like so:

Mapping the Return Type of SPROC Methods to Data Model Classes

In the "CustOrderHist" SPROC example above the stored procedure returned a sequence of product history results containing two columns of data: the ProductName of the product, and the Total Number of orders the customer has made for that product.  The LINQ to SQL designer automatically defined a new "CustOrderHistResult" class to represent this result.
We could alternatively choose to have the return result of a SPROC map to an existing data model class we have already defined in the LINQ to SQL designer (for example: an existing Product or Order entity class). 
For example, assume we have a "GetProductsByCategory" SPROC in our database that returns product information like so:

Like before we can create a "GetProductsByCategory" method on our DataContext that calls this SPROC by dragging it onto our LINQ to SQL designer.  Rather than just dropping the SPROC anywhere on the designer, though, we'll instead drop the SPROC on top of the existing "Product" class in our data model designer:

This gesture of dropping the SPROC onto the Product class tells the LINQ to SQL designer to have the "GetProductsByCategory" method return a sequence of "Product" objects as a return result:
 
One of the cool things about having our SPROC return "Product" objects like above is that LINQ to SQL will automatically track the changes made to the returned Product objects just like it would Product objects returned via LINQ queries.  When we call the "SubmitChanges()" method on our DataContext, the changes we have made to these objects will automatically be saved back to the database.
For example, we could write the code below to retrieve (using a SPROC) and change the price of all products within a specific Category to be 90% of their current value:

When we call SubmitChanges() at the end it will transactionally update all of the product prices.  To understand more about how change tracking and the SubmitChanges() method work, as well as about how Validation Business Logic can be added to data model entities, please read my LINQ to SQL Part 4: Updating our Database tutorial. 
In my next blog post in this LINQ to SQL series I'll also cover how you can replace the dynamic insert/update/delete SQL generated by the ORM with custom SPROCs that handle the database updates instead.  The nice thing is that the code above wouldn't change at all if I configured my DataContext to use SPROCs for updates - it would purely be a mapping layer change and the code written against my data model would be oblivious to it. 

Handling SPROC Output Parameters

LINQ to SQL maps "out" parameters in SPROCs as reference parameters (ref keyword), and for value types declares the parameter as nullable.
For example, consider the below "GetCustomerDetails" SPROC which takes a CustomerID as an input parameter, and which returns the company name as an output parameter in addition to its order history as a query result:

If we drag the above SPROC onto our "Order" class in the LINQ to SQL designer, we could then write the below code to call it:
VB:

C#:

Notice in the code above how the SPROC helper method returns back a sequence of Order objects - but also then returns the CompanyName as an output parameter to the helper method.

Handling Multiple Result Shapes from SPROCs

When a stored procedure can return multiple result shapes, the return type of the SPROC method on the DataContext cannot be strongly typed to a single class shape.  For example, consider the SPROC below which returns either a product result or an order result depending on the input parameter:

LINQ to SQL supports the ability to create SPROC helper methods that can return either a Product or Order shape by adding a partial "NorthwindDataContext" class to the project that defines a method (which in this case we'll call "VariablesShapeSample") that invokes the SPROC and returns an IMultipleResult object like so:
VB:

C#:

Once this method is added into our project we can then call it and convert the result to be either a Product or Order sequence when we are using it:
VB:

C#:

Supporting User Defined Functions (UDFs)

In addition to SPROCS, LINQ to SQL also supports both scalar-valued and table-valued user defined functions (UDFs), as well as the in-line counterpart to both.  Once added to your DataContext as a method, you can use these UDF functions within your LINQ queries.
For example, consider a simple scalar user defined function called "MyUpperFunction":

We can drag/drop it from the Visual Studio Server Explorer onto our LINQ to SQL Designer to add it as a method on our DataContext:

We can then use this UDF function inline within our LINQ expressions when writing queries against our LINQ to SQL data model (notice it is being used within the "where" clause below):
VB:

C#:

If you use the LINQ to SQL Debug Visualizer that I blogged about here, you can see how LINQ to SQL transforms the above expression queries into raw SQL that execute the UDF inside the database at runtime:

Summary

LINQ to SQL supports the ability to call Stored Procedures and UDFs within the database and nicely integrate them into our data model.  In this blog post I demonstrated how you can use SPROCs to easily retrieve data and populate our data model classes.  In my next blog post in this series I'll cover how you can also use SPROCs to override the update/insert/delete logic when you SubmitChanges() on your DataContext to persist back to the database.
Hope this helps,
Scott
Published Thursday, August 16, 2007 2:16 AM by ScottGu
Filed under: , , , ,

Thursday, February 2, 2012


花开叶落几春秋,往事如烟云。故地今朝又重游,昔日多少欢愁 如梦中。
小桥楼阁依旧在,物是人已非。问君几多离别畅,恰似孤雁秋去又春来。