Archive

Archive for March, 2011

Get Value Of Session variables from Generic Handlers

March 26, 2011 3 comments

if we are trying to access the value of session variables from Generic Handlers(.ashx files) like we acces from codebehind (.cs file) as the code below


public class MyHandler: IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";

string sessionValue = Convert.ToString(HttpContext.Current.Session["LoginUser"]);
}
}

we got NullReferenceException or Object reference not set to an instance of an object.

Solution

To overcome this exception and to access session variables from the HttpHandler, we should inherit the interface IReadOnlySessionState or IRequiresSessionState
(For this include the name space System.Web.SessionState first).

IReadOnlySessionState provides read-only access to session variables. And

IRequiresSessionState provides read/write access to session variables.

The working code is like this

using System.Web.SessionState;
public class MyHandler: IHttpHandler,IRequiresSessionState {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";

string sessionValue = Convert.ToString(HttpContext.Current.Session["LoginUser"]);
}
}

Categories: ASP.NET

Change Master Page Dynamically using c#

March 20, 2011 Leave a comment

We all knows and use the concept of masterpage in web applications.Recently i found a way to change the master page dynamically from codebehind like this

protected void Page_Preinit(object sender, EventArgs e)
{
string filename = "~/MasterPages/MyMaster.master";
if (filename != "")
{
MasterPageFile = filename;
}
}

Categories: Prsnl References

Read web.config settings values from .aspx page

March 20, 2011 Leave a comment

We know how to read the values (may be our connection string, or some frequently used values) from Web.config file on codebehind
string settingvalue = System.Configuration.ConfigurationManager.AppSettings["UrlPath"];
But if we want to use the same idea on .aspx page we can use code like this

Read web.config settings values from .aspx page

Read web.config settings values from .aspx page


and in web.config file add
Read web.config settings values from .aspx page

Read web.config settings values from .aspx page

Categories: Prsnl References

excel automation/Com Addin using c# .screenshots

March 20, 2011 2 comments

The normal screen shot of Office Excel is shown here.You can see the built-in features like Home,Insert,PageLayout etc on the top of the excel sheet..

Excel Automation addin using c#

Excel Automation addin using c#


Now we can add our own tab say sound Vibration Functions –A set of user defined functions for doing some sound related calculations
Excel Automation addin using c#

Excel Automation addin using c#

The main idea behind this product is that create our own formulas say
a_to_v(AccelerationToVelocity)
a_to_d(AccelerationToDisplacement) etc
and register these formulas on excel .Now we can use this formulas from the Insert Function wizard of Office Excel Like accessing the inbuilt formulas like financial, Date&Time etc.

 Excel COM Addin using c#

Excel COM Addin using c#



Working of Some Formulas using these pluggin

Formula1
Select cell A1 and press formula Freq_1_1 and press on OK Button. The cells are filled with results based on the conditions.
Excel Automation Addn using c# and Excel COM Addin using c#

Excel Automation Addn using c# and Excel COM Addin using c#

If you are using normal function wizard for filling multiple cells u need to select the no of cells, presss control+shift+enter keys to get the results filled and again there are some disadvantages like we need to know the no of cells the results may going to fill and make selection accordingly and if the no of cells selected is more like if we want result in 5 columns and we select 10 column then we get #NA in the remaining columns etc…
With this approach all these are getting reduced.
Select cell C1 and press formula Freq_1_1 and press on OK Button with horizontal direction u get calls filled in horizontal direction.
Select cell C3 and press formula Freq_1_1 and press on OK Button with vertical direction and with units u get calls filled in horizontal direction.

Excel Automation Addn using c# and Excel COM Addin using c#

Excel Automation Addn using c# and Excel COM Addin using c#

Formula2
Suppose if we want to calculate the formula on n no of cells .The result is appear like this

Excel Automation Addn using c#

Excel Automation Addn using c#

The same thing can achieve using the ribbon by following 2 simple steps
Select the cell we need the result
Select the formula and select the cells and click oK button

Excel Automation Addn using c# and Excel COM Addin using c#

Excel Automation Addn using c# and Excel COM Addin using c#


Benefits
Save lot of time
Easy maintenance – if u want to remove the cells inbetween it is very difficult using the normal method.but using this pluggin its very easy

Copy a column Data Of Datatables using C# to another DataTable

March 16, 2011 Leave a comment


DataTable dtData1 = new DataTable();
//write code to fill this datatable

DataTable dtData2 = new DataTable();
//create structre and data of datatable1 to datatable2
dtData2 = dtData1.Copy();

//remove unwanted columns.If the column name is not known u can use the column no
dtData2.Columns.Remove("Col1");
dtData2.Columns.Remove("Col3");
dtData2.Columns.Remove("Col5");
dtData2.Columns.Remove("Col7");

//Rename the Columns to the new names if needed
dtData2.Columns["Col2"].ColumnName = "Col1";
dtData2.Columns["Col4"].ColumnName = "Col2";
dtData2.Columns["Col6"].ColumnName = "Col3";

//save changes
dtData2.AcceptChanges();

Categories: Prsnl References

Get Order & Name Of a DataTable Column

March 16, 2011 1 comment

DataTable myDataTable=new DataTable();
//To get the name of a column .If u dont know the column name use column no
string currentColumnName = myDataTable.Columns["Col1"].ColumnName;
//To get the Rank Or Column No
// then pick the column no of this column and save it as the value
int rank = myDataTable.Columns[currentColumnName].Ordinal;

Categories: C# Programming Tips

Find Percentile Of N Numbers using c#

March 16, 2011 Leave a comment

In Excel we had a formula to find the percentile of n numbers for a given percentile value…I  want to implement the same in some place of my code  and when i am looking for an algorithm  i cant able to find any satisfactory solution and i decided to write an algorithm of my own.

The basic definition of Percentile and examples to find percentile can be found here
Link For Percentile

Note:Accuracy of this algorithm (Deviation of 0.0001 from the result of excel sheet percentile function)
Download Code From here and copy paste to your editor
find percentile using c#

Categories: C# Programming Tips