Thursday, August 2, 2012

How to write a left outer join in LINQ?

This can be accomplished by using DefaultIfEmpty(). See the example LINQ query below:

var provQuery = (from a in entities.Table1
                join c in entities.Table2 on a.ID equals c.ID into temppvcon
                from t2 in temppvcon.DefaultIfEmpty()
                where a.ID == Id
                select new { a.ID, a.NAME, t2.ID, t2.NAME });


Check out the SQL generated from the above LINQ query:

SELECT
1 AS "C1",
"Extent1"."ID" AS "ID",
"Extent1"."NAME" AS "NAME",
"Extent2"."ID" AS "ID",
"Extent2"."NAME" AS "NAME"
FROM  (SELECT
      "TABLE1"."ID" AS "ID",
      "TABLE1"."CON_ID" AS "CON_ID",
      "TABLE1"."NAME" AS "NAME",
      "TABLE1"."ADDED_DT" AS "ADDED_DT"
      FROM "DB"."TABLE1" "TABLE1") "Extent1"
LEFT OUTER JOIN (SELECT
      "TABEL2"."NAME" AS "NAME",
      "TABEL2"."PART" AS "PART",
      "TABEL2"."ID" AS "ID",
      "TABEL2"."CLUSTER_ID" AS "CLUSTER_ID"
      FROM "CDCDATA"."TABEL2" "TABEL2") "Extent2" ON "Extent1"."ID" = "Extent2"."ID"
WHERE ("Extent1"."ID" = :p__linq__0)


Hope this is clear. Enjoy...

How to view the SQL generated by LINQ to Entities?

There is a ToTraceString() method for objectQuery. The anonymous object returned from the LINQ query has to be casted into ObjectQuery as shown below to use the ToTraceString():

var provQuery = from a in entities.Table1 where a.ID == id orderby a.Name
                            select a;

The SQL generated from the LINQ can be viewed by using as below:

((System.Data.Objects.ObjectQuery)provQuery).ToTraceString()

You can copy from watch window and run the sql query to test it.

Thursday, July 19, 2012

Crystal Report Error in web application for reports

Error as below:

The description for Event ID 4353 from source Crystal Reports cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event:

The keycode assembly, BusinessObjects.Licensing.KeycodeDecoder.dll, cannot be loaded.


Solution:


In Page Setup for that report check the "No Printer" option. This worked out for me.

Tuesday, June 19, 2012

How to create a numeric only Text box

Add the following code in the textBox_KeyPress event:

If  (Not Char.IsNumber(e.KeyChar) And (e.KeyChar <> ChrW(Keys.Back))) Then

      e.Handled = True
End If

Column Names in upper case for Oracle subquery

Oracle is case-insensitive for table and column names in queries but the data dictionary records names in uppercase.

check out this example below:

select User_Number, User_Description from (
select user_id as "User_Number", User_name as "User_Description" from users_tbl);

The above query returns error as below:

ORA-00904: "USER_DESCRIPTION": invalid identifier
00904. 00000 -  "%s: invalid identifier"

Corrected query:

select USER_NUMBER, USER_DECSRIPTION from (
select user_id as "USER_NUMBER", User_name as "USER_DECSRIPTION" from users_tbl);

Friday, June 8, 2012

Same parameters used multiples times in Oracle Query

If we use same parameters multiple times in a query in oracle as shown in example, it errors out. We need to create another set of variables with same values.


Check the usage of parameters in the below example:
:fromDate
:toDate
:uname


:fromDate1
:toDate1
:uname1



:fromDate2
:toDate2
:uname2


All the above paramaters are having the same values.


For example:-
select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description
FULL JOIN

select user_name,call_reason from call_mrs_tbl, users_tbl where call_placed_dt between :fromdate1 and :toDate1
and user_name = :uname1 order by user_description
FULL JOIN

select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate2 and :toDate2
and user_name = :uname2 order by user_description;

We cannot use the above query as below. It errors out saying invalid datatype.


select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description
FULL JOIN
select user_name,call_reason from call_mrs_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description
FULL JOIN
select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description;











Order of parameters matters in oracle query

Faced lots of time to figure out why my oracle query was erroring out. It was the order of the dynamic parameters in the query should match the order we pass the parameter with values.

For example:-
select user_name,call_reason from call_tbl, users_tbl where call_placed_dt between :fromdate and :toDate
and user_name = :uname order by user_description;

Here you can see three parameters :fromDate, :toDate & :uname. These parameters needs to be passed from C# using command object in the following order in which it is used in the query, which is as below:
1. :fromDate
2. :toDate
3. :uname

I was passing the parameters in the following order:


1. :uname
2. :fromDate
3. :toDate


Took me a while to figure out. These are some tips which can be used if you stumble across such error again, you need not waste much time to figure out.

Hope this helps others.

Thursday, April 26, 2012

Programming Test


Given a list, array, of integers, find the index of the two consecutive numbers which have the largest sum.
For example, if the array is 1, 2, 3, 4, 5, the answer is 4, as 4+5 = 9.

Wednesday, April 11, 2012

How to format a phone number/Fax number in crystal reports formula

Use Picture({field.PhoneNum},"(xxx) xxx-xxxx").

How to check if a string field contains a character in crystal reports formula

You can use Instr({text.field},'-') > 0 - indicates there is a character in the string field..
if  Instr({text.field},'-') = 0 - indicates there are no such characters in the string field.