Friday, November 27, 2009

Linq : Case conditions in Asp.net c#

 public object GetAllImageDetailsByMasterId(int masterkey)  {    GeneralDataContext db = new GeneralDataContext();    var objImgDetail = from p in db.ImageDetails    where p.ImageMasterKey == masterkey                        select new {                                                           LargeImagePath = p.LargeImagePath,                                                        SmallImagePath =                        (                       p.SmallImagePath == 
null ? p.LargeImagePath : p.SmallImagePath ==
"" ? p.LargeImagePath : p.SmallImagePath) }; return objImgDetail.ToList(); }

Asp.net Linq Sql : Multiple left join with Paging c#




Left out Join implementation in Linq.. hmm first I have no idea how to proceed.
Almost in all the attempt the syntax errors used to appear... and there is no 
relevant information available online from google search.
After giving a long try and error finally i was able to use Linq for
 Left outer join and implemented Page indexing too.
The screen shot explains of "Sales" object has many relations with other tables and
 the function given below will explain the how is implement left join.

public static List<Object> GetSaleList(int pageIndex, int pageSize)
{
GeneralDataContext db = new GeneralDataContext();
var ObjSale = (from S in db.Sales
join Prop in db.Properties on S.PropertyId equals Prop.PropertyId
   into tempProp from PROP in tempProp.DefaultIfEmpty()
       join ImgD in db.ImageDetails on S.ShowImageID equals ImgD.Id 
   into tempImg from IMGD in tempImg.DefaultIfEmpty()
join Countries in db.Countries on S.TargetCountry equals Countries.ID
   into tempCountry from COUNTRIES in tempCountry.DefaultIfEmpty()
join Curr in db.Currencies on S.Currency equals Curr.Id
   into tempCurrency from CURRENCIES in tempCurrency.DefaultIfEmpty()                       
select new { S.Id,S.PropertyId,S.Link,S.ImageId,S.TargetCountry,S.Currency,
     S.SellingPrice,S.Sell,S.Rent,S.Lease,
PROP.PropertyType,IMGD.SmallImagePath, COUNTRIES.COUNTRY1,
       CURRENCIES.CurrencySign
});

ObjSale = ObjSale.Skip((pageIndex - 1) * pageSize).Take(pageSize);
return ObjSale.ToList();


}

Tuesday, November 24, 2009

Database search MSSQL



set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER PROCEDURE [dbo].[proc_KeySearch]
(

@keyword varchar(256)
)
AS




DECLARE @SearchStr nvarchar(100)
SET @SearchStr = @keyword
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')
WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName = ''
SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)
WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
AND TABLE_NAME = PARSENAME(@TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar',
'numeric','decimal', 'double', 'money')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)

IF @ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)
FROM '
+ @TableName + ' (NOLOCK) ' +
' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
)
END
END
END
SELECT ColumnName, ColumnValue FROM #Results
DROP TABLE #Results

Monday, November 16, 2009

ReSharper 5.0 and Visual Studio 2010

Resharper for VS 2010 is about to come soon. Like me many of programmers are not using VS2010 because of Resharper.

More about Resharper pulgin

Resharper from wikipedia
From JetBrain Blog
For Resharper benefits go through these demo
Demos



Friday, November 13, 2009

iframe 100% height width in javascript

JavaScript



function resize_iframe(frameid) {
if (frameid != null && frameid != 'undefined') {
var height = window.innerWidth; //Firefox
if (document.body.clientHeight) {
height = document.body.clientHeight; //IE
}
document.getElementById(frameid).style.height = parseInt(height - document.getElementById(frameid).offsetTop - 8) + "px";
}
}

// this will resize the iframe every
// time you change the size of the window.
window.onresize = resize_iframe;




 
HTML :








 








Src : http://guymal.com/mycode/100_percent_iframe/

Tuesday, November 10, 2009

Google Area chart in Asp.net

Stunning charts on the web page makes the user to stare on the screen for long time and use it more often. Yes charts really convey many messages in one shot.

To implement charts in Asp.net we have 2 best options.
1. Microsoft Charts
2. Google Chart API.

Microsoft Charts :
* Advantages:
1. Easy to use.
2. No third party components.
3. Attractive and pleasing.
4. Quality of charts are superb.
5. Offcourse it is free. :-) and no lic prob for this chart controls.
6. Supports complex charting and drill down charts and reports.

* Disadvantages
1. For more complex charts Server side code is complex and difficult.
2. Comparatively slow but the slowness is worth for the information and UI.

You can have more info from here on MS Charts Installing MS chart adding into vs 2008 toolbox and using MS chart with sample code

Google Charts :
Reference links : Google Chart Reference

*Advantages:

1. Very simple to design for simple charts
2. Light weighted and faster
3. Code is easy to understand and attributes are less to customize.
4. No installation setup is required works on javascript.
5. It is free.
* Disadvantages:
1. Since api works on online javascript so browser should be javascript enabled.
2. Presence of internet is a must.

Both has its own advantages and shortcomings but as a developer we must know what is required for us is the best.



Before generating a chart and displaying it was a nightmare and those who like to choose to develop or spend time on charts was used to called a "Hero of Developers" no matter whether it is a 3rd party component or develop by using HTML, tr, td, images, div and css combinations.

Google Chart Api like Google map api has a made a chart development tremendously easier and faster which almost a fresh student also can make it.

Here I am showing a simple Area graph using Google Chart API. It is so easy that anybody will feel no need of explanation on the html. It requires no serverside code only javascript.

Happy coding "Hero of Developers".






<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["areachart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Actual Targets achieved');
data.addColumn('number', 'Projected Targets');

data.addRows(9);
data.setValue(0, 0, '2000');
data.setValue(0, 1, 2);
data.setValue(0, 2, 0);

data.setValue(1, 0, '2001');
data.setValue(1, 1, 30);
data.setValue(1, 2, 25);

data.setValue(2, 0, '2002');
data.setValue(2, 1, 45);
data.setValue(2, 2, 70);


data.setValue(3, 0, '2003');
data.setValue(3, 1, 85);
data.setValue(3, 2, 80);

data.setValue(4, 0, '2004');
data.setValue(4, 1, 100);
data.setValue(4, 2, 0);

data.setValue(5, 0, '2005');
data.setValue(5, 1, 20);
data.setValue(5, 2, 0);

data.setValue(6, 0, '');
data.setValue(6, 1, 20);
data.setValue(6, 2, 0);

data.setValue(7, 0, '');
data.setValue(7, 1, 20);
data.setValue(7, 2, 0);

data.setValue(8, 0, '');
data.setValue(8, 1, 20);
data.setValue(8, 2, 0);


var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, { width: 600, height: 340, legend: 'bottom', title: ' Performance ' });
}
</script>
</head>

<body style="font-family:Calibri;">
<div id="chart_div"></div>
</body>
</html>

Sunday, November 8, 2009

Friday, November 6, 2009

Jquery Ajax Message reading techniques

It is a time where almost every Asp.net web developer is aware of Jquery, its usage and ajax.
Using Jquery Ajax in Asp.net with JSON is no more a hot topic.



This is one of the ajax example below.


function LoadDropDown() {
$.ajax({
type: "POST",
url: "http://localhost/Line/JqueryMethods/JqueryAjax.aspx/LoadCountries",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("<option value='0'>Select</option>").appendTo('#<%=ddlCountries.ClientID %>');
$('#<%=imgCountry.ClientID %>').attr("Src", ("Common/_assets/img/flags/XX.png"));
for (var i = 0; i <= msg.d.length - 1; i++) {
$("<option value='" + msg.d[i].ID + "'>" +
  msg.d[i].COUNTRY1 + ":" + msg.d[i].TLD + "</option>").appendTo('#<%=ddlCountries.ClientID %>');
                    }
}
});
}

here I want to show how to read the return value of the server side method.
I had a big issue once with asp.net 2.0 , 3.5 and different version of jquery.
so if any one have issues on how to read the return values then here is below
note: above example is reading a List<> object.

1. msg
function(msg) {
$('#Divid').html(eval(msg)) ;
},
2. msg.d
function(msg) {
$('#Divid').html(eval(msg.d)) ;
},

3. eval('(' + msg + ')').d
function(msg) {
$('#Divid').html(eval('(' + msg + ')').d)
},


I spent more than 4 hrs on this ways because of no clue, whether using 3.5 sp1, or different versions of jquery. But just using "debugger;" we can easily findout which is suitable.

Happy coding ;)