Wednesday, 22 August 2007

The ultimate Off-roader!

At last I have bought the vehicle that I have been wanting to take to masinagudi for the past 15 years - the Gypsy! And no surprise I made full use of the gypsy in my first trip! Check the photos below..

Modified my Honda Civic

I have done some face lifting to my honda civic in India, It was done by Kitup Rajiv.

Tuesday, 21 August 2007

Passing NameValueCollection to WebService

There might be a need to pass in a NameValueCollection class object into a webService. Since you can only pass in serialized objects into a web service it is not a straight forward process. One of the ways to do it is to have a Class that stores the name value pair and add it to an arraylist object. Then pass the arraylist.ToArray() object to the webservice. The other way is to use a string array. I have shown the code to do it using a string array below.

This is the method that takes in a NameValueCollection and returns an Array
/// This Method converts the NameValueCollection of all the parameters
/// into an arraylist of string arrays

private Array ConvertNameValueCollectionIntoArrayList(NameValueCollection nvcParams)
ArrayList aList = new ArrayList();
string[] strNameValue;

IEnumerator paramsEnum = nvcParams.GetEnumerator();

int i = 0;
while (paramsEnum.MoveNext())
string name = nvcParams.GetKey(i);
string value = nvcParams[name];

strNameValue = new string[2];
strNameValue[0] = name;
strNameValue[1] = value;

return aList.ToArray();

Now this Array can be passed into the web service and de-decoded as shown below..


//The ArrayListObject below is the array that contains the arraylist
[XmlInclude(typeof(string[]))] //This line is very important
public void Initiate(Object[] ArrayListObject, string .....)
ArrayList al = new ArrayList(ArrayListObject); ParameterValue[] Params = ConvertNameValueCollectionIntoParams(al);

private ParameterValue[] ConvertNameValueCollectionIntoParams(ArrayList nvp)
ParameterValue[] parameters = new ParameterValue[nvp.Count];
for (int i = 0; i < nvp.Count; i++)
ParameterValue parameter = new ParameterValue();
string[] strNameValuePair = (string[])nvp[i];
parameter.Name = strNameValuePair[0];
parameter.Value = strNameValuePair[1];
parameters[i] = parameter;
return parameters;

Tuesday, 14 August 2007

Quick Tips C#

This post will contain a list of quick tips and good practices in c# that I use..I will be updating this list as I find new things...If you have anything that you would like to contribute, please post them in the comments...cheers

1. Use string.IsNullOrEmpty() - short and sweet to check whether a string is null or empty

2. goto keyword, this is a very useful keyword that can be used to skip certain lines of code in a method -

Monday, 13 August 2007

Highlighting Row in GridView

I recently had a situation where I had to highlight a row in the gridview if the value of the column Today was 0. Found out there was a very easy way to do it using the OnRowDataBound Event of the GridView, the code's below..

protected void GridViewID_OnRowDataBound(object sender, GridViewRowEventArgs e)
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
DataRowView drv = row.DataItem as DataRowView;
string fieldName= drv["FIELDNAME"].ToString();
if (fieldName.Equals("0"))
row.CssClass = "YourCssClassName";

DataFormatString - Formatting Data in a GridView -HTMLEncode to false

The data in a datagrid can be formatted using the DataFormatString. For example you can add a £ sign or make sure the numbers are formatted to 2 decimal points and so on. The link below gives you a reference list of the things you can do but the most important point is, you need to set the HtmlEncode Property to false, or else you will not get the desired results. This one can get you because it is not stated clearly anywhere and spent half an hour trying to figure out why it wasnt working!

Programmatically Adding Columns to GridView

There are cases where you might want to use the same GridView to display diferent columns in a page. This is possible by programmatically adding the columns using the BoundField Object. The key thing to note here is the diference in adding a column to a DataGrid and a GridView, to add a bound column in the DataGrid you should use the BoundColumn object and to add a column to the GridView, you should use a BoundField object. You can create the gridview in your .aspx page and set the sorting and things like that or you can create the complete gridview in your code behind using the GridView class.
The code below shows how to add columns to a gridview that was created in the .aspx page..

--.aspx page--

AllowPaging="True" AllowSorting="True" PageSize="21" EmptyDataText="No data available."
CssClass="gridstyle" GridLines="None" OnSorting="GridViewID_Sorting" OnPageIndexChanging="GridViewID_Paging"

--.cs page--

private void DefineGridViewColumns()
BoundField column = new BoundField(); //You can use the TemplateField object to add a template column
column.HeaderText = "XX";
column.DataField = "ID";
column.SortExpression = "ID";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(7);

column = new BoundField();
column.HeaderText = "RANK";
column.DataField = "RANKNO";
column.SortExpression = "RANKNO";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(7);

if (TimeSpan.Equals("TW"))
column = new BoundField();
column.HeaderText = "THIS WEEK";
column.DataField = "XX_TARGET";
column.SortExpression = "XX_TARGET";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(10);

Wednesday, 1 August 2007

Error "The permissions granted to user '\IUSR_' are insufficient in Reporting Server

If you get this error "The permissions granted to user '\IUSR_' are insufficient for performing this operation." when you work with Reporting Server, it is because the reporting server's virtual directories under IIS is running in 'Annonymous Mode'. To fix this open IIS, right click on the virtual directory that your report server uses, click on the tab 'Directory Settings' and click edit under 'Authentication and Access Control' and untick the 'Enable Anonymous Access'.