30 May 2012

Temporary table issue in RDLC

I have just created a RDLC report using a Stored Procedure, which uses a temporary table.  I am getting error "Could not generate a list of fields for the query. Invalid object name '#Tablename'".  To fix this add below statement in Stored Procedure before creating temporary tables.
"SET FMTONLY OFF"
Make your temp table a permanent one and do the management of it yourself (deleting rows after use etc). This could be faster as well as indexable and you can add keys as needed to ensure integrity. It can also act as a cache for long running queries.


18 May 2012

Data Cache in ASP.NET

Most of the times we need to store large amounts of data client side is by using the ViewState. It will only exist on the page you add it to though. So if you are jumping around from page to page it won't really work. In that case you really should be using Session or Application Cache depending on your scenario.


ViewState:  
 ViewState is one of the techniques that Asp.net provides in order to save the data for latter use. The important fact about the ViewState is that it can only save the values for page level access. It means that if I save something on page1.aspx using Viewstate I cannot access it on the page2.aspx using the same technique. For this reason ViewState is only used to save the values for the page level access which should be maintained after post backs.Whenever we write anything in the ViewState object a hidden field is created in the page. After the post back it will display the value of the dataset using the ViewState control.

So if you are doing something where you are always on the same page and just doing many PostBacks for things like paging or sorting then ViewState will work fine but do realize you will be passing large amounts of data back and forth to the server each time a PostBack is made.
Example Using ViewState:

// Set it
ViewState["dsViewEmp"] = dsEmployee;


// Get it
DataSet ds = ViewState[" dsViewEmp "] as DataSet;


Session and the Application Cache are accessed the same way. Just replace ViewState with the word Session or Cache.


What is Data Cache?
Data Cache is used to storing frequently used data in the Cache memory. It's much efficient to retrieve data from the data cache instead of database or other sources. We need use System.Web.Caching namespace. The scope of the data caching is within the application domain unlike "session". Every user can able to access this objects.


How data caching is work?
When client request to the server, server execute the stored procedure or function or select statements on the Sql Server database then it returns the response to the browser. If we run again same process will happen on the web server with sql server.
In the very first time retrieve the records from the sql server database and stored into the Cache memory.  After that records will retrieve from Cache memory (IIS). We can set expiration period time after expiration time over the caching again it will hit the sql server database and stored into the cache memory and next retrieve will happen on the data cache memory.


Advantages of Cookies
Following are main advantages of using cookies in web application:
It's very simple to use and implement.
We can avoid database hitting.
We can reduce network traffic.

How to create data cache? 
Cache["Employee"] = dsEmployee


We can create data caching use Cache Keyword. It's located in the System.Web.Caching namespace. It's just like assigning value to the variable


How to Read data from Data Cache?
Dataset dsEmployees = (Dataset) Cache ["Employee"]


This is very similar to assigning object to the Dataset.


Where does Data Cache are stored?
This is one of the interesting things to find out the Data Caching in your local drive. First of all, From "IE Internet Option ", delete Browsing History to clear Cache. 


How to remove a Data Cache? 
We can remove Data Cache manually.

//We need to specify the cache name


   Cache.Remove(String key);



Conclusion:
When it comes to choosing between ViewState and Caching it all comes down to what you are doing. ViewState is an ideal candidate if you are planning to save small values. Mostly values of server controls are saved in ViewState. If you plan to store large datasets, datatables or collections than data caching is an excellent candidate.


ReferenceLinks:
1. http://www.highoncoding.com/Articles/69_View_State_vs_Data_Caching.aspx
http://msdn.microsoft.com/en-us/library/z1hkazw7%28VS.80%29.aspx






05 May 2012

Using MID() to Cut Out a SubString

I have a filed that begins with numeirc characters and it ends with alpha character (like 0125461E). I want to alpha (E) part form the string and leave the remaining (0125461) field value.

we need to "cut" it out of our  IDNumber field.
Our example field is called {USERS. IDNumber}.
Now the way we cut strings out of other strings is with the MID() function. The MID function takes three parameters. The string on which to operate, where to start and the length of the string to return.

MID(String, Starting Character Position, Length of String to Return)

Supposing that {USERS. IDNumber } is going to return Justin Hill, we can see the result of various MID statements:
MID({USERS. IDNumber }, 8, 1) then we will get "E"
Also note that you may omit the final parameter (length) to return "the rest" of the string, like this:
MID({USERS. IDNumber  }, 8) 

Example:

//Declaring variable to display message...
Stringvar message;
if (MID({REP. IDNumber},  8 ,1)="E")
then message:="Employee" 

Pass value from main report to sub report

I am designing a report which includes sub report every thing is fine but i have to display the main report field value in sub report . To get the main report vale in to the sub report follow the bellow steps.
Step 1:
1. In main report create a formula, name it, edit it with the formula editor as follows:
Shared numbervar NAME := {Rep.Sno} 
Save formula.  Note that you should replace the name and type of variable and a formula with ones of your own
2. Now create a formula in your subreport, name it, edit it with the formula editor as follows:

Shared numbervar NAME;
NAME
3. Use formula created in step 2 in your report (drag drop in the report from formula fields.
Note that subreport is processed after the main report, so it's easier to pass values from main report to subreport.

01 May 2012

Display a serial number in RDLC report

I have developed a report in reportviewer using ASP.NET. I want to display serial number in report follow the below steps. In RDLC design drag and drop a text box from the toolbox and right click on it and select Expression as shown below.


An expression containing the RowNumber function, when used in a text box within a data region, displays the row number for each instance of the text box in which the expression appears. This function can be useful to number rows in a table.
Using RowNumber(Scope) function to display the row numbers. If scope is nothing then RowNumber(Nothing) provides the running count of rows in the outermost data region.

How to Change Date Format in .RDLC reports (MM/dd/yyyy)

In my Report Design i want to show date in the format of (MM/dd/yyyy). The column has the value in dd/MM/yyyy.  The output on the webform looks like this: 05/18/2012 to do this follow the below steps.
Step1: In RDLC design right click on the column/textbox and then it will open context menu as shown below then select the Expression option from the menu.
Step 2:When click on Expression then it will open a windows as shown below. Change the Expression statement as shown and then click on OK button.


RDLC report Text box/Column value formating

In my RDLC reports i want show 2 decimal points. Decimal value like 25.5000 now i want to show it in 2 decimal point in report (25.50). To do as shown below.
Use the "Round" function on your column or Textbox.
Some thing like  =Round(FieldName,2) 
where FieldName is the name of your column or the value you want to round off and 2 is the decimal places

Time Out downloading large files

When your downloading a table data in the format of excel sheet, while doing that it throws time out error due to lack of enough size. To download the file add the following tag in the web.config file as shown below.
Problem:

By default, Machine.config is configured to accept HTTP Requests upto 4096 KB (4 MB) and it is reflected in all your ASP.NET applications. You can change the Machine.config file directly, or you can change only the Web.config file of the application(s) you want to.
Open your Web.config file, and just below the <system.web> tag, add the following tag:

  <system.web>
    <httpRuntime executionTimeout="90"
            maxRequestLength="4096"
            useFullyQualifiedRedirectUrl="false"
            minFreeThreads="8"
            minLocalRequestFreeThreads="4"
            appRequestQueueLimit="100"
            enableVersionHeader="true"
    />
Now, just take a look at the maxRequestLength="4096"  and   executionTimeout="1000" attribute of the  <httpRuntime>  tag. As you may have realized, all you need to do is change the value to some other value of your choice (8192 for 8 Mb, 16384 for 16 Mb, 65536 for 64 Mb, and so on...).