Thursday, December 10, 2009

LINQ - Simple examples



LINQ where - operation

int[]array ={1,2,3,4,5,6,7,8};

var between5 = from N in array where N < 5 select N;

foreach(var elements in between5)
{
Response.write(elements);
}

// Transforming the array into ToList

ToList - LINQ

string[] name ={"Senthil","Murugesan","Karthick"};
var Name = from SP in name order by K select K

var namelist=Name.ToList()
foreach(var item in namelist)
{
Response.write(item);
}



// Loading XML document in LINQ

var record = from e in XElement.Load(Server.Mappath("students.xml").Element("Name") select e.Element("Name");
foreach(students in record)
{
Response.write(students);
}

webservice consuming through jQuery - parameter

I have a web service which will check the user name and password against database.I used to send 2 parameters one is user id and password.If both are matched then status will be comes

$.ajax({
type: "POST",
url: "LoginService.asmx/AuthenticateMethod",
data: "{'UserName': '" + $("#TextBox1").val() +"', 'Password': '" + $("#TextBox2").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg)
{
$("#status").html(msg.d);
}
});

Wednesday, December 09, 2009

GridView with ModalPopupextender in C#












onrowdatabound="GridView1_RowDataBound">
















Code behind to send selected index as parameter to webservice to retrieve record
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lk = (LinkButton)e.Row.FindControl("lnk");

lk.Attributes.Add("onclick", "ShowMyModalPopup('"+s+"')");
}
}

Monday, December 07, 2009

Self Code testing - FxCop 1.36

I wanted to test my codes are written correctly what Microsoft's defined.So I prepared a class library for Banking Transaction.Before testing this my BankTransaction.dll

Simply I test my codes against Design Rules,Naming Rules,Interoperability Rules,Performance and Portability Rules.

My codes were

using System;
using System.Collections.Generic;
using System.Text;

namespace SP.MyBankTransaction
{

public class BankTransactionClass
{
private string account_holder_name;
private string account_number;
private double deposit_amount;
private double withdraw_amount;
private double balance_amount;
public string AccountHolderName
{
get { return account_holder_name; }
set { account_holder_name = valu; }
}
public string AccountNumber
{
get { return account_number; }
set { account_number=value; }
}
public double WithdrawAmount
{
get { return withdraw_amount; }
set { withdraw_amount = value; }
}
public double DepositAmount
{
get { return deposit_amount; }
set { deposit_amount =value; }
}

public double BalanceAmount
{
get { return balance_amount; }
set { balance_amount=value; }
}
public double DepositTrasaction(string FromAccount, double AmountToBeDeposited)
{
FromAccount = AccountNumber;
AmountToBeDeposited = DepositAmount;
return BalanceAmount + AmountToBeDeposited;

}
}

}






Resolution were

Correct the spelling of 'Trasaction' in member name 'BankTransactionClass.DepositTrasaction(string, double)' or remove it entirely if it represents any sort of Hungarian notation.

in line 43,Method name has been misspelled,I changed.Notice the "Transaction".
in line 42,Parameters were Pascal-cased word,I changed to camel cased.
in line 16,Compound Word "AccountHolderName" changed as "Accountholder.

My corrected Code is

using System;
using System.Collections.Generic;
using System.Text;

namespace SP.MyBankTransaction
{

public class BankTransactionClass
{
private string account_holder_name="";
private string account_number="";
private double deposit_amount=0.0;
private double withdraw_amount=0.0;
private double balance_amount=0.0;

public string Accountholder
{
get { return account_holder_name; }
set { value = account_holder_name; }
}
public string AccountNumber
{
get { return account_number; }
set { value = account_number; }
}
public double WithdrawAmount
{
get { return withdraw_amount; }
set { value = withdraw_amount; }
}
public double DepositAmount
{
get { return deposit_amount; }
set { value = deposit_amount; }
}

public double BalanceAmount
{
get { return balance_amount; }
set { value = balance_amount; }
}
public double DepositTransaction(string fromAccount, double amountToBeDeposited)
{
fromAccount = AccountNumber;
amountToBeDeposited = DepositAmount;
return BalanceAmount + amountToBeDeposited;

}
}

}

Wednesday, December 02, 2009

Gridview row deleting in C#

Steps to delete a row in Grid view programmatic which bind with real time database table.
1,In Grid view server control must be set to false of AutoGenerateColumns
2,Make Columns tag to list the user defined column header on the asp.net page.
just like the following
  
DataKeyNames="ProductID" SelectedIndex="1"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowcommand="GridView1_RowCommand" AutoGenerateColumns="false">








3,Set to DataKeyNames value to Table's primary key like
  
DataKeyNames="ProductID" SelectedIndex="1"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowcommand="GridView1_RowCommand" AutoGenerateColumns="false">


An event named "onrowcommand" triggered on the Grid view deletion of the selected row must be deleted from the table.

Here I used the filter CommandName and commandArgument to check whether the deletion link button which attached the GridView clicked.


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Remove")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];

Database db = DatabaseFactory.CreateDatabase("DatabaseConnectionString");
DbCommand cmd = db.GetSqlStringCommand("DELETE from Products where ProductID='" + row.Cells[1].Text.ToString() + "'");
db.ExecuteNonQuery(cmd);
GridView1.DataSource = ShowProductDetail();
GridView1.DataBind();

}

}


I used Microsoft Enterprise Library 4.1 in my example to deal with database and its related operation.Its based on SQLExpress database.