Monday, 25 January 2010

JQuery merge or combine two tables into one

Hi, this is something interesting isn't it? But, this is what my requirement. I will tell you in detail. I have a gridview on my page and another table with the same number of columns. Grid view is for showing and updating data. And I have one extra table for adding a new record. This table immediately follows the actual grid view. The extra table named the class as "tableAdd". So, user sees the gridview header and enter the data into the tableAdd row and hit save button and gridview will refresh and shows up the newly created record.

But there is a problem in look and feel. Because both are in different tables, when the browser width changes or resolution changes the columns in both tables are not in sync, even if they have same columns and same width. So, how to make them sync? Then I got an idea to implement a solution  using JQuery. So, I planned to grab both tables on client side and merge them into one.

<table class="gridview"><tr><td>col1</td><td>col2</td></tr></table>
<table class="tableAdd"><tr><td><input type="text" /></td><td><input type="button" /></td></tr></table>

Now, above are two tables I have and now I need to merge them into one and delete the second table from the DOM.

$(".gridview > tbody:last").append($(".tableAdd > tbody").html());   
$(".tableAdd").remove();

Hope, you understood it well and may helps in some scenarios. Please let me know, if you have any problems.

JQuery Append a table row at first and last positions of a table

This is something you need to know when you have some requirement to add rows to table on client side using some java script technology. I have chosen JQuery and the requirement is, I need to add the rows at first and last of the existing table. Below is the way to achieve that.

Add as first row of a table:
$(".gridview").prepend("<tr></tr>");
Add as last row of a table:
$(".gridview > tbody:last").append("<tr></tr>");

I think, you feel it's simple. Yes it is easy and helps in solving some issues. And remember the class gridview I have used in this example, is the table class name. And the string "<tr></tr>" is the actual table row content.

Hope this helps and let me know, are there any better ways to implement the same.

Saturday, 23 January 2010

Do not allow HTML into the textbox

This is the most of times QA team will try to do and file bugs in web applications. They tries to enter the HTML into the textbox and the request fails as usual. The page renders with an .net generic exception if it is asp.net web application. This is because of the security problems. ASP.NET [OR I am not sure how different languages treat the html in textbox] if any HTML in the input textbox then it treats that it as "script injection attack".
If you think that the web application is safe to enter HTML tags in the input controls then there are two solutions.
  • For the specific page, I mean in the page directive just add extra attribute ValidateRequest="false". This will apply to only that page, so you can enter HTML into the text boxes for that page.
  • If you want to solve this problem for all pages in the application then in the web.config file, add ValidateRequest="false" for <pages> tag.
But, as we discussed this is not the 100% true solution, because there are chances of script injection attack. So, how to solve this problem? Today these days, everyone started using javascript or JQuery in their web applications. I have chosen JQuery to fix this problem. Below is the solution.
$(document).ready(function() {
$("input").live("keyup", function() {
RemoveTheHTMLFromTextBox($(this));
});
$("input").blur(function() {
RemoveTheHTMLFromTextBox($(this));
});
$("input").live("click", function() {
RemoveTheHTMLFromTextBox($(this));
});
function RemoveTheHTMLFromTextBox(obj) {
var inputValue = $(obj).val();
if (inputValue.indexOf('<') > -1 || inputValue.indexOf(">") > -1) {
$(obj).val($(obj).val()
.replace(/"/g, "")
.replace(/</G, ??)
.replace(/>/g, "")
.replace(/&/g, ""));
}
}
});

This will look for any HTML tags [<>] and replace them with empty space. This solution will work perfectly. Hope you like it. What is your opinion? any best solution?

Note: Don’t forget to add the JQuery file before you access this script.

How to check JQuery object is null

This is simple and no need to write post for it. You may feel same right? But, this is the question I received from almost 10 people till now. So, I thought of writing a post on it. So that, it may help people who are looking for the same through out the world.

By default JQuery returns object when you declare $("$id") or $(".class"). If you write code for it as below, then that's wrong.

if($("#id") != null)
{
//Write code if the object is not null
}

if($(".class") != null)
{
//Write code if the object is not null
}

If you write the above code for checking null condition then if condition always success and will execute code inside of the if condition. So, the correct solution is,

if($("#id").length > 0)
{
//Write code if the object is not null
}

if($(".class").length > 0)
{
//Write code if the object is not null
}

It tries to find all objects which has the given id or class and if any exists then the length will be equal to the number of times the id or class occurred. So, if the length is zero then it's nothing but the id or class don't exist and the object is null.

Hope, this solved your problem and this is a new tip for today. Is this helpful?

Sunday, 17 January 2010

Refresh intellisense local cache in Sql Server 2008

Everyone knows that Sql server 2008 has an excellent feature for supporting the intellisense. This will surely fasten the development. Last week, I have created 3 tables in my Sql server 2008 and when I try to write the stored procedure based on those tables, surprisingly they weren't shown in the intellisense any more. I thought initially it's a bug in Sql Server 2008. Lately after done some research and found that we need to refresh the cache for intellisense to shown up everything correct. Please find the below article which is more helpful and detailed.

http://cherupally.blogspot.com/2009/12/how-to-get-sql-server-2008-intellisense.html

Friday, 15 January 2010

Report viewer control authentication – Part 2 – Forms Authentication

If the reporting services configured to use forms authentication and you need to show the reports in the custom developed applications then the need of processing authentication the report viewer control through the code.
Report viewer control authentication using windows authentication is described in the part 1 here. Now, we will discuss the authenticating forms authentication through C# code.
If we are are developing windows application and want to use report viewer control, then we need to implement the below logic to authenticate.
reportViewer1.ServerReport.ReportServerCredentials.SetFormsCredentials(null, "userName", "password", "");
this.reportViewer1.RefreshReport();
Where as in forms authentication simply assigning the credentials not enough. The reasons behind are, security, code efficiency, performance, response time etc everything come into the picture. So, we need to write code which supports everything.
What are the major tasks?
  • Report viewer control excepts the credential of type IReportServerCredentials. So, we need to create an object which inherits from this interface and pass this to the report viewer control to authenticate.
  • Handling cookie. Based on the login and request is successful we will write the cookie to browser and keep that in browser for further requests processing. The advantage is if cookie is available then won’t make any request to report server for authenticating.
  • To create the cookie related information we actually need of hijack the request and response and get the cookie information and save it to browser. So, how to catch the request and response which made to report server? We will discuss this later in this article.
  • To actually communicate to the report server, we need to make the communication with it. The best way to do that is using web services. Everyone knows that reports in SSRS gets with two sites. One is report manager means report web application [/reports] and the report server means a report web service[/reportserver]. So, we will use the web service, write a proxy and implement the existing functions in it.

I think, it is little bit complex to understand the above tasks. But actually they are very simple to implement.


Code needed: We need two classes to get what we need. These are custom classes and add them in single file named ReportServerCredentials.cs somewhere in the project.
  • Classes needed - ReportServerCredentials and ReportingService.
public class ReportServerCredentials : IReportServerCredentials
{
private Cookie m_authCookie;
public ReportServerCredentials(Cookie authCookie)
{
m_authCookie = authCookie;
}
public WindowsIdentity ImpersonationUser
{
get
{
return null; // Use default identity.
}
}
public ICredentials NetworkCredentials
{
get
{
return null; // Not using NetworkCredentials to authenticate.
}
}
public bool GetFormsCredentials(out Cookie authCookie,
out string user, out string password, out string authority)
{
authCookie = null;
user = ConfigurationManager.AppSettings["ReportServerUserName"];
password = ConfigurationManager.AppSettings["ReportServerPassword"];
authority = "";
return true; // Use forms credentials to authenticate.
}
}
public class MyReportingService : rsExecutionReference.ReportExecutionService
{
private Cookie m_authCookie;
public Cookie AuthCookie
{
get
{
return m_authCookie;
}
}
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.CookieContainer = new CookieContainer();
if (m_authCookie != null)
request.CookieContainer.Add(m_authCookie);
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
WebResponse response = base.GetWebResponse(request);
string cookieName = response.Headers["RSAuthenticationHeader"];
if (cookieName != null)
{
HttpWebResponse webResponse = (HttpWebResponse)response;
m_authCookie = webResponse.Cookies[cookieName];
}
return response;
}
}
A small explanation:
ReportServerCredentials class is the class which is inheriting from the IReportServerCredentials interface.
  • If we are using the impersonation then the first property will be used.
  • If we are passing the custom network credentials then the second property will be used.
  • If we are using forms authentication then the method GetFormsCredentials() will be used.
ReportingService is the class which is inheriting from the ReportExecutionService web service. The question in your mind now is what is the "rsExecutionReference". This is the web reference to the report server web service. To get this, right click on the web application/web site and select the option "Add web reference". Now, in the input box, enter the report server url ending with "ReportExecution2005.asmx". For example, if your report server is at http://localhost/reportserver then the web service location will be http://localhost/reportserver/ReportExecution2005.asmx. Add the web reference to project or site. Now, we have the web service proxy created in the solution. So, in my code rsExecutionReference is nothing but the web service proxy for the report server web service.
The web service has two methods already implemented GetWebRequest() and GetWebResponse(). So, we are overriding them in our code to catch the cookie ticket information which is returned by the report server. If you observe the GetWebResponse() code, we are actually checking for the header information from the response and in the response we are catching the cookie we need. If cookie is null means the request authentication failed and means invalid credentials passed.
I think, till now what I have mentioned is clear for you and we are done with contacting or communicating with the web service. Now, where we are calling the web service or in other words where the authentication request started? It's not yet. Check the below code and notes for it.
I have a Utility class in my application where I place all the util related code in it. For example purpose, I am assuming this method you have placed in Utility class  [If no Utility class in your application then create one and place this below method in it].
public static HttpCookie AuthenticateReportServerAccess()
{
MyReportingService svc = new MyReportingService();
svc.Url = ConfigurationManager.AppSettings["ReportServerWebSeriveUrl"];
HttpCookie hcookie = null;
try
{
svc.LogonUser(ConfigurationManager.AppSettings["ReportServerUserName"],
ConfigurationManager.AppSettings["ReportServerPassword"], null);
Cookie myAuthCookie = svc.AuthCookie;
if (myAuthCookie == null)
{
//Message.Text = "Logon failed";
}
else
{
hcookie = new HttpCookie(myAuthCookie.Name, myAuthCookie.Value);
HttpContext.Current.Response.Cookies.Add(hcookie);
}
}
catch (Exception ex)
{
//Message.Text = "Logon failed: " + ex.Message;
}
return hcookie;
}
What this method is doing? A little explanation is here. We need to make or send a request to report server for authenticating the current request to load a report. So, I am using this method for that. I have a proxy class as described above and based on that I have created my own custom class named ReportingService. So, I am using that to make the call by using the built in function exists in the web service named LogonUser(). Which is actually takes three parameters named username, password and authority. Authority is optional, the specific authority to use when authenticating a user. For example, a Windows domain, some name to make distinct the call for that user. Pass a value of null to omit this argument. This will make call to the report server. Because of we override the request and response methods in proxy, it will come to those method and executes all the code in them. So, in the method AuthenticateReportServerAccess() we are making request and getting the cookie. If wrong credentials passed then it means cookie is null. So, you can write your own logic there like throw exception or show some message, If you have any login page implemented for reporting then redirecting the user there etc… If cookie exist then Add that cookie to the response object.
Now, we are done with making a call and processing it and loading cookie. Now what? What we left with… See below.
In the page where we have placed the report viewer control, there add the below code. The below code you can write may be in onclick event of something or when page loads depends on your requirement.
HttpCookie cookie = Request.Cookies["sqlAuthCookie"];
if (cookie == null)
{
cookie = Util.AuthenticateReportServerAccess();
}
if(cookie != null) {
//Add logic to pass parameters if needed.

reportViewer1.ServerReport.ReportServerUrl = new Uri("reportserverurl");
reportViewer1.ProcessingMode = ProcessingMode.Remote;
reportViewer1.ServerReport.ReportPath = "reportPath";
Cookie authCookie = new Cookie(cookie.Name, cookie.Value);
authCookie.Domain = Request.Url.Host;
reportViewer1.ServerReport.ReportServerCredentials =
new ReportServerCredentials(authCookie);
//If any parameters then reportViewer1.ServerReport.SetParameters(reportParams);
}
What is happening in the code? We are checking whether the cookie already exist means user is already authenticated and if it null then we are sending request to report server and loading the cookie to browser. If cookie exists then it will go to second if condition and loads the report in the report viewer control by passing the cookie information to report server.
OHHH… What else? we are done with complete coding. We are left with testing the code.
Note: If you observe the code, there are some places we are getting the appsettings keys from the web.config.
Hope you are well understood and not with many questions in mind at this stage. Please feel free to ask me if you have any questions. Love to hear comments.

Report viewer control authentication – Part1 – Windows Authentication

Report viewer control, I think everyone knows what it is and how to use it. Today, I want to tell you how to authenticate the report viewer control in ASP.NET web application or windows application.

The report viewer control passing the credentials is same in both windows and web applications. How to pass windows credentials to the report viewer control?

  • Either you can set the <identity impersonation="true" /> or
  • You can directly pass the credentials object to the report viewer control as shown below. If default credentials then
    System.Net.ICredentials credentials = System.Net.CredentialCache.DefaultCredentials;ReportServerCredentials rsCredentials = serverReport.ReportServerCredentials; rsCredentials.NetworkCredentials = credentials;

    But, in some scenarios, you may need to pass the windows credentials dynamically instead of the logged in user credentials. Then it's the time to use NetworkCredentials object. Check below.

    ICredentials credentials = new NetworkCredential("User name", "Password", "Domain");

    Now, assign this credentials object to the report viewer credentials. So, I prefer to not directly give the credentials directly in the code. Instead add them in the web.config and access them in the code.

    I think, you understood it well. Love to hear comments.

Sql Server reporting services and Forms authentication – Part 3 – Linking aspnetdb to report server

I think, you have read my previous two posts related to implementing forms authentication in report server.

Sql server reporting server and forms authentication – Part 1 – Understanding the concept.

Sql Server reporting server an forms authentication – Part 2 – How to implement.

I just followed Russell’s article to link sql server report server to aspnetdb. So, I prefer to redirect my users to the same post. I don't want to repeat the whole story here again. So, please go here and continue your reading.

http://blogs.msdn.com/bimusings/archive/2005/12/05/using-sql-reporting-services-2005-and-forms-authentication-with-the-whidbey-2-0-sqlmembershipprovider.aspx

Hope with this post, you are completely done how to use, implement and linking the default aspnetdb to the report server in forms authentication.

Sql Server Reporting services and forms authentication – Part2 - How to implement?

Before continue this post, please read the post understanding the concept forms authentication in sql reports – Part1. The implementation of the process is so simple.

  • Microsoft team already given a sample application which has the forms authentication enabled.
  • They have written some code which has a security extension with login pages.
  • To understand it well or to implement just do this. Go here and download the sql server samples from Codeplex. http://www.codeplex.com/SqlServerSamples/Release/ProjectReleases.aspx?ReleaseId=4000
  • The file SqlServerSamples.msi contains all the example types for sql server.
  • Download the file and start installing them.
  • After installed the file successfully, go to the location where it installed [Usually if it is sql server 2005 then the location would be C:\Program Files\Microsoft SQL Server\90.] and the folder named "samples".
  • And in the samples folder we need to consider the sample "Samples\Reporting Services\Extension Samples\FormsAuthentication Sample".
  • This sample has all the code we needed.
  • Now, before proceed open the Readme file found in the location mentioned above.
  • This is the time we actually start the implementation. Please do as is mentioned in the readme file without anything missed.

The above process will take around half an hour to configure everything correct. And if we do everything correct, then you are able to see the report server running using forms authentication.

What to remember while implementing the process?

  • The connection string in the sample code. The sample code contains has the connection string as shown below.

    using (SqlConnection conn = new SqlConnection("Server=localhost;" +
                  "Integrated Security=SSPI;" + "database=UserAccounts"))

  • So be sure you installed the useraccounts database in the default instance of the sql server and the windows account you have logged in has access to it.

Now, you have everything installed and setup the forms authentication in sql server reports.

Hope you enjoyed the post. Please let me know your feedback and any questions if you have.

Sql Server reporting services and forms authentication - Part1

Till now, I know Sql Server reporting services supports only Windows authentication. And of course till now What I did for all clients is the same. The roles or access everything will define using the default report manager site and deploy that on the client environment. But, the current project I am working on is completely different and the main requirement is enabling forms authentication in the sql server reporting services. The story behind is, the client is running different applications based on the aspnetdb [Forms authentication]. So, they want us to run the report server using forms authentication based on the same database. So, how to achieve that? This was the question in my mind till last week. After done research about two days found some nice articles and the way to achieve that. So, I thought of explaining the research, analysis and the way we need to proceed in this article to my readers. So, if you have any requirement like this in future just go ahead and start the work in the same process as explained below. This Part1 describes all about understanding the concept.

To understand the concept:

Here you need to understand one thing that Reporting services in SQL server which comes by default only supports Windows Authentication. There is no default way to make it or turn on forms authentication. To do this, we need to make some custom configurations, changes to web.config and custom coding. But, nothing to worry. It's easy. :)

Reporting services are developed to be extensible. You can write your own extensions and deploy or integrate them to reporting services by making some changes to their config files. So, now we need to concentrate on what is a security extension? Before proceed we need to understand some key points:

  • Reporting services uses role-based security system to authorize users.
  • Security extension supported is based on an open and existing API.
  • Use custom defined authentication or forms authentication if any only if, you need to define different users and roles which is not based on windows accounts.
  • Read the security extension API in the documentation given in the above link. This has all the classes and interfaces defined to start coding.

Now, what is the security extension. "A Reporting Services security extension enables the authentication and authorization of users or groups; that is, it enables different users to log into a report server and, based on their identities, perform different tasks or operations."

So from this post I hope you understand what we need to do to enable forms authentication in reporting services.

Sql Server Reporting services 2008 Tablix control

When working with Sql server 2008 reporting services I found a new control named Tablix. [Of course there are plenty of new features] This is a mix of both Table and a Matrix. The main and great advantage of it is when we drag a table or matrix to the report body you can't find table or matrix any more. The report control automatically convert it to Tablix. Now, it's very easy to show the complex and grouping of data with this control.

The interest thing in 2008 reporting services is, When I open report builder 2008, BIDS 2008 I didn't find the Tablix control at all and started researching for the control. Stupidly searched in google like download tablix control ssrs 2008 etc… But, once I drap and drop the table or matrix and right clicked for properties found "tablix properties" in list of properties there. Then understood the whole concept and implemented the reports very fast.

Hope this post will help you understand it. Let me know what you think.

Thursday, 14 January 2010

Sql Express instance is not in the list of network servers

Today, I have a requirement to work with SQL Server express. So, I setup a server with Sql server express installed and configured everything we needed. My team has 4 developers and everyone should access the sql server express to run the project. But, when they try to access the sql server express instance the server is not responding and always it results an error. When I open network servers, I found only the instances of my sql sever and in that list I didn't find the new sql server express I have setup on the server nb11. See the below screen.

image

So, This stop us working together and started finding what's the problem. I logged into nb11 machine and checked all the services related to Sql Server and found Sql Server Browser status is stopped. Because of this, it is not able to expose to external environment or outside of the server.

So, the solution is just start running the Sql server browser. Now, go to list of server in network servers and you started seeing the nb11 express instance there.

Now, the question is what is Sql Server Browser? Simple definition is "Provides SQL Server connection information to client computers."

Hope you like this post and please let me know, if you have any questions.

Report project template in Visual Studio

This is the question most of the people asking. So, just thought of posting it for my readers.

Report project template is associated with the reporting services in the Sql server. This template is  not an addin to visual studio or a specific download won't be available. You will see the template if and only if you install client tools while installing the Sql Server. So, to get that template, open the sql server setup file and select the client tools option from the list of features available. That's it!!! Let me know, if you have any issues to questions.

Wednesday, 6 January 2010

Disable button in onclick and process the button click event

This is what I need to implement for one of my project. I am using Ajax and if user tries to hit the submit the button more than once, only the very first request should submit to server and all other requests shouldn't make any request. So, for this I need to disable the button when first  time click on button and enable it after processed. But, I faced a problem that when I try to disable the button in javascript of client side click event then the postback event not raising at all. There are couple of ways we can implement this. First, I tried from server side code.

Solution 1:

btnSubmit.Attributes.Add("onclick","javascript:" + btnSubmit.ClientID + ".disabled=true;" + this.GetPostBackEventReference(btnSubmit));
But, this solution won't work in all scenarios and especially if there are validations.

Solution 2:

OnClientClick='javascript:this.disabled=Page_ClientValidate("");' UseSubmitBehavior="false"
For ASP Button the two properties onclientclick and usesubmitbehavior will do magic for us. In onclientclick event I am disabling the button if and only if the validation passed. Go back to my previous post. It describes how to validate the controls in client side and give the result true or false. And because of using the UseSubmitBehavior property it will automatically re-enable the button once the request processed.

Hope this gives you the enough idea. Like to know what you think. 

Data Formatting in sql server reporting services

In Sql server reporting services the formatting the data is pretty much simple as we have plenty of built in options available for percentage, numbers, decimal, currency etc. So, just them to data format and display the data as you want. Below are most commonly use format for the usual reports.

  • c – Currency
  • d – Decimal
  • n – Number
  • p – Percentage.

If you want two decimal places in decimal number then in the format option [Text box properties -> format] then the  format should be d2. In percentage if you want two decimal places in percentage then the format is p2 etc.. But, I have tried it some reports, and found not working. Then used the default C# string formats for the data formatting in reports. That is, for two decimal places the format is like 0.00. Then the result data will be two decimal places. Example, 5.67. So, you can try both and use which one is best fit for you.

There are other plenty of formats available and they are completely described here.

http://msdn.microsoft.com/en-us/library/ms252080%28VS.80%29.aspx

This help you to understand the data formatting in SSRS. What do you think?

Fix to Divided by zero problem in Sql server reports

When we work with reports, most of the times we need to do complex calculations and logic. When we do calculations, there are chances of using the division operation. So, need of checking the divided by zero problem every time. So the solution I propose is, write a simple custom function and call it wherever needed. For every report there is a part called Code and there we can write the custom VB code.

Public Function CheckDividedByZero(ByVal param1 As Double, ByVal param2 As Double) As Double
If param2 = 0 Then
Return 0
Else
Return param1 / param2
End If
End Function

So, call this function wherever you are performing the division operation. Hope this will solve your problem and you never face the divided by zero problem any more.

Date formatting in Sql server reporting services

When we work with reporting services, we come across with different requirements to display the date in different formats. So, we need to know the formats we can give to get required value. Sql server reporting services supports different formats as T-sql and C# does. So, I just want to give the valid formats I use to display the date in required format.

=Format(Fields!DateCreated.Value, "M/d/yy") - 3/14/1986
=Format(Fields!DateCreated.Value, "d-MMMM-yy") - 14-March-86
=Format(Fields!DateCreated.Value, "MM/dd/yyyy") - 03/14/1986
=Format(Fields!DateCreated.Value, "M/d/yyyy H:mm") - 3/14/1986 22:10
=Format(Fields!DateCreated.Value, "dd-MMM-yyyy") - 14-March-1986
=Format(Fields!DateCreated.Value, "MMM-dd-yyyy") - Mar-14-1986
=Format(Fields!DateCreated.Value,"dd-MMM-yy") - 14-Mar-86

You can try different combinations of above to get desired output. Hope this helps and love to hear the comments.

Scheduling jobs in Sql server Express

I know, at some point you need this when you use sql express. Sql express is the limited edition of Sql server. So, it doesn't contain all the features. To schedule we need sql server agent service. But it comes only with sql server. So, creating jobs are not possible with sql express. So, is there any way? Yes. Today I found a nice article on it. And that is perfect.

Take a look at it and let me know your ideas on it.

http://www.sqlteam.com/article/scheduling-jobs-in-sql-server-express

Hope you like this article. Don't you?