Wednesday, July 9, 2014

Windows Forms Application versus Web Application

 Comparison between Windows Forms and ASP.NET Web Applications.


Windows Forms
ASP.NET Web Application
User Interfaces, data binding etc.
Easy to build
Difficult to build
Deployment and Maintenance
Complex. New versions of assemblies, configuration files, and other required files must be deployed on all client machines. Usually user interaction required.
Easy. Need to deploy assemblies and configuration files on the server only. Transparent to the client.
Performance
Faster
Slower
Robustness and Reliability
One client machine goes down, other users are still live.
Usually web servers are never down. However if the server goes down, all users are affected.
Network Congestion
Depending on the data transfer and connections made to the server from various clients.
Depends
Resources
Runs on the client machine.
Runs on a Web server.
Catastrophic failure
User interaction required.
Usually user interaction not required.
Framework dependency
All client machines have to install required versions of .NET framework and other required libraries.
Only server needs to have .NET framework and other required libraries.

Thursday, May 1, 2014

Behavior Based Safety (BBS) is the "application of science of behavior change to real world problems".[1] BBS "focuses on what people do, analyzes why they do it, and then applies a research-supported intervention strategy to improve what people do".[2] At its very core BBS is based on a larger scientific field called Organizational behavior management.[3]
To be successful a BBS program must include all employees, from the CEO to the front line workers. Including but not limited to hourly, salary, union employees, contractors and sub-contractors. To achieve changes in behavior, a change in policy, procedures and/or systems most assuredly will also need some change. Those changes cannot be done without buy-in and support from all involved in making those decisions.
BBS is not based on assumptions, personal feeling, and/or common knowledge. To be successful, the BBS program used must be based on scientific knowledge.


How to Integrate Facebook in your ASP.Net website



Facebook is one of the top rated social media networking sites that impress everyone. More than 270 million users are using Facebook. 
Facebook connect -- this enables users to integrate Facebook platform to your website. This allow a user to connect with your site using the Facebook account and can share posts on your pages with friends on Facebook. The connection is established between Facebook and your website using a trusted authentication.
Before integrating Facebook on your website, you need to follow these steps:

Setting Up with Facebook

You’ll need to set up your application with Facebook. Set-up is free and happens pretty much instantaneously, because there’s no application approval process to go through.

1. Register your website with Facebook.

 

Log in to Facebook and visit the developer’s application at Facebook.com/developers. Here you can set up new applications and edit existing ones. You can also access SDK documentation and interact with the Facebook developer community.


In App Name: give the name of your site and continue. It will give you the Unique App ID/App Secret.



The URL of your website and the website URL you registered with Facebook should be same.

Note: If you want to access some additional information like Email ID etc, you have to use OAUTH (which is authorize the user and provide grant permissions to access).

2. Add Facebook DLL Refrences to your website/bin folder


  • Facebook.dll  
  • Facebook.web.dll

3. Web.config 

 

 Add the App ID/App Secret  value 
 <appSettings>
    <add key="redirect_uri" value="<your_redirect_uri>">
    <add key="AppKey" value="<your_App_ID>"/>
    <add key="AppSecret" value="<your_App_Secret>"/>
 </appSettings>

 

 4. Add Facebook login button in your login.aspx page


<asp:Panel ID="pnlLogin" runat="server">
                    <a href="https://www.facebook.com/dialog/oauth?client_id=your_app_id&redirect_uri=your_redirect_uri">
                        <img src="../../images/f_login.png"  />
                    </a>
 </asp:Panel>
<a id="lbllogout" runat="server" onserverclick="lbllogout_Click" visible="false" > </a>


For accessing some additional information like offline_access,email etc, you have to add scope feature in login like

<a href="https://www.facebook.com/dialog/oauth?client_id=your_app_id&redirect_uri=your_redirect_uri&scope=offline_access,user_status,publish_stream,email,manage_pages,user_groups">


It will ask you for allowing the permissions. Click Allow.

 5. Now add code in login.aspx.cs to access Facebook user details in your page


using Facebook.Web;
using Facebook;

string getAccessToken = "";
WebClient client = new WebClient();

 protected void Page_Load(object sender, EventArgs e)
  {
           string fbCodeGiven = Request.QueryString["code"];
            if ((fbCodeGiven != null))
            {
                WebRequest AccessTokenWebRequest = WebRequest.Create("https://graph.facebook.com
                 /oauth/access_token?client_id=" + your_App_ID + "&redirect_uri=" +
                 your_redirect_uri  +  "&client_secret=" + your_App_Secret"] + "&code=" +
                 fbCodeGiven);

                StreamReader AccessTokenWebRequestStream = new
                StreamReader(AccessTokenWebRequest.GetResponse().GetResponseStream());
                string WebRequestResponse = AccessTokenWebRequestStream.ReadToEnd();
                getAccessToken = WebRequestResponse.Substring(13, WebRequestResponse.Length -
                                                13);

                Session["getAccessToken"] = getAccessToken;
                string url, userInformation, email = null, CorrectEmail = null, id = null,
                first_name = null, last_name = null;

                Regex getValues;
                Match infoMatch;
                string username = "me";
                url = "https://graph.facebook.com/" + username + "/" + "?access_token=" +
                          getAccessToken;

                userInformation = client.DownloadString(url);
                getValues = new Regex("(?<=\"email\":\")(.+?)(?=\")");
                infoMatch = getValues.Match(userInformation);
                email = infoMatch.Value;
                CorrectEmail = email.Replace("\\u0040", "@");

                getValues = new Regex("(?<=\"id\":\")(.+?)(?=\")");
                infoMatch = getValues.Match(userInformation);
                id = infoMatch.Value;
                Session["facebookuserID"] = id;

                getValues = new Regex("(?<=\"first_name\":\")(.+?)(?=\")");
                infoMatch = getValues.Match(userInformation);
                first_name = infoMatch.Value;

                getValues = new Regex("(?<=\"last_name\":\")(.+?)(?=\")");
                infoMatch = getValues.Match(userInformation);
                last_name = infoMatch.Value;
            }
}

protected void lbllogout_Click(object sender, EventArgs e)
{
            if (Convert.ToString(Session["facebookuserID"]) != "")
            {
                string getAccessToken = Convert.ToString(Session["getAccessToken"]);
                Session.Remove("facebookuserID");
                Response.Redirect("https://www.facebook.com/logout.php?next=" + your_redirect_uri
                                                 + "&access_token=" + getAccessToken);
                Session.Remove("getAccessToken");
            }
 }

The URL supplied in the next parameter must be a URL with the same base domain as your application as defined in your app's settings.

IMPORTANT NOTE -- You must replace "your_App_ID", "your_App_Secret" with the APP ID, APP Secret you find in your application details in the Developer application on Facebook!

Download Files  loginwithfacebook.rar

6. Testing 


Now we are done with the coding. Its time for testing.

  • Run your Website 

I hope this article will give you good knowledge about integration with Facebook into your ASP.Net website. Please share your feedback and comments with us. 

Thursday, February 20, 2014

How to clear all controls in asp.net using c#

private void ClearInputs(ControlCollection ctrls)
        {
            foreach (Control ctrl in ctrls)
            {
                if (ctrl is TextBox)
                    ((TextBox)ctrl).Text = string.Empty;
                else if (ctrl is DropDownList)
                    ((DropDownList)ctrl).ClearSelection();

                ClearInputs(ctrl.Controls);
            }
        }


ClearInputs(Page.Controls);

Difference between Inherits vs Src vs Codebehind & AutoEventWireUp

The @ Page Directive
The @ Page directive specifies the Web Form attributes that are enabled, disabled, or the specific values, such as the transaction attributes of a Web Form. The ASP.NET Framework uses these attributes to provide the specific capabilities of a Web Form when the page is rendered. This directive is also used by Visual Studio .NET to track related project files.
Inherits
The Inherits attribute defines the code behind class that the Web Form inherits. This can be any class that derives from the System.Web.UI.Page class. The Inherits attribute is only necessary when using code behind. If the class does not exist, then a system exception will be thrown. If you are using code embedded in <script runat=”server”></script< tages, the Inherits attribute is not required.
Src
The Src attribute specifies the name of the code behind file (the file on the file system where the class code exists) to use when dynamically compile when the Web Form is requested. The ASP.NET Framework will find the class file, and use a Just-In-Time (JIT) compiler to complie the class.
The Src attribute and the Inherits attribute are not mutually exclusive – if you use the Src attribute you must still specify the class to inherit from in the Inherits attribute, even if the code behind file only has one class in it. The ASP.NET Framework will always look in the assembly for the class first. If the class exists in the compiled assembly, then it will be used. If the class is not in the assembly then the ASP.NET Framework will JIT compile the class from the Src file, ad use it.
Visual Studio .NET does not use the Src attribute. Rather, Visual Studio .NET expects that all Web Form code behind classes will be compiled into an assembly. The Build menu, Build option will create the assembly and put the DLL in the bin directory.
CodebehindThis is the big confusing one…Codebehind. The Codebehind attribute is NOT an ASP.NET attribute, it is a Visual Studio .NET attribute. The only thing the ASP.NET Freamework knows about this attribute is that it should be ignored. Visual Studio .NET uses the Codebehind attribute to track the class file that is related to the Web Form. This enables Visual Studio .NET to automatically embedd code in the class file when the design environment chenges, such as when a new server control is placed on the Web Form.
One of the biggest misconceptions is that replacing the Codebehind attribute with the Src attribute will solve the problem when you get an error stating that the Inherits class can not be found. While this does remedy the problem, the belief of why is incorrect. Swapping Codebehind for Src just removes the ability for Visual Studio .NET to embed code in the related class file, and enables JIT compilation for the Web Form. Generally this error is the result of NOT using the Src attribute, and not building the assembly. To resolve this error, either build the assembly, or add the Src attribute.
If you are not using the Src attribute, you must rebuild the assembly for any class changes to take affect.
The advantage to using pre-compiled assemblies, and not the Src attribute is that the source code files do not have to be put on the web server. All the class logic resides in the assembly, so only the DLL needs to be distributed, not the source code.
AutoEventWireUpThis is a freebie! The AutoEventWireUp attribute, set to true by default, specifies whether or not the Page events are auto-wired. In other words, when the value is true, the Page events are auto-wired, so that Page_Load(), Page_PreRender(), etc. event handlers work with no extra coding. When set to false you must explicitly create set the Page event handlers to methods with signatures that match the event signature.
Visual Studio .NET sets this value to false, and uses the Page_Init() event handler (which is always auto-wired) to invoke an InitializeComponent() method, where the Page event handlers get defind, as well as the event handlers for any server controls on the Web Form.
Summary
Ok, in short, here it is.
  • The Inherits attribute specifies the System.Web.UI.Page derived class to inherit from.
  • The Src attribute can be used to enable JIT compiling of the code behind class, but the ASP.NET Framework will always look in the compiled assembly for the class first. The Inherits attribute MUST be used with the Src attribute.
  • The Codebehind attribute is used by Visual Studio .NET to track the codebehind class file. This class is ignored by the ASP.NET Framework.
  • The AutoEventWireUp attribute specifies whether the Page event should be auto-wired or not.

scope_identity() and current_identity()

DENT_CURRENT is similar to the SQL Server 2000 identity functions SCOPE_IDENTITY and @@IDENTITY. All three functions return last-generated identity values.
However, the scope and session on which last is defined in each of these functions differ:
  • IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.
  • @@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
  • SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.

Thursday, February 13, 2014

Difference between Asp.Net MVC and Web Forms

asp.net and mvc
Asp.net framework is a part of .net platform for building, deploying and running web applications. Now, we can develop a web application by using Asp.Net Web Form and Asp.Net MVC. In this article, I am going to expose the main difference between Asp.Net Web Form and Asp.Net MVC.
Difference between Asp.Net MVC and Web Forms
Asp.Net Web Forms
Asp.Net MVC
Asp.Net Web Form follow a traditional event driven development model.
Asp.Net MVC is a lightweight and follow MVC (Model, View, Controller) pattern based development model.
Asp.Net Web Form has server controls.
Asp.Net MVC has html helpers.
Asp.Net Web Form has state management (like as view state, session) techniques.
Asp.Net MVC has no automatic state management techniques.
Asp.Net Web Form has file-based URLs means file name exist in the URLs must have its physically existence.
Asp.Net MVC has route-based URLs means URLs are divided into controllers and actions and moreover it is based on controller not on physical file.
Asp.Net Web Form follows Web Forms Syntax
Asp.Net MVC follow customizable syntax (Razor as default)
In Asp.Net Web Form, Web Forms(ASPX) i.e. views are tightly coupled to Code behind(ASPX.CS) i.e. logic.
In Asp.Net MVC, Views and logic are kept separately.
Asp.Net Web Form has Master Pages for consistent look and feels.
Asp.Net Web Form has Layouts for consistent look and feels.
Asp.Net Web Form has User Controls for code re-usability.
Asp.Net MVC has Partial Views for code re-usability.
Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access.
Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing interactive web application with latest web standards.
Visual studio and Visual web developer(free) are tools for developing Asp.Net Web Forms.
Visual studio and Visual web developer(free) are tools for developing Asp.Net MVC application.
Asp.Net Web Form is not Open Source.
Asp.Net Web MVC is an Open Source.