Tuesday 17 November 2015

All About Pointers

Definition:In computer science, a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its address. A pointer references a location in memory means a pointer points to address.
When we declare int num=20; then this declaration tells the C compiler to :
1- Reserve space to hold Integer value.
2- Associate a name (here num) with this memory location
3- Store the value 20 at this location.

So by above figure it is cleared that location number is also a number itself. It is not fixed so it may change when you re-run your application.

Address can not be negative and that is the reason for using unsigned integer or %u as format specifier.
Any discussion is incomplete without program. So let us write a program to print a variable's value and its address:

#include<stdio.h>
int main()
{
      int num=20;
      printf("Value of num is = %d",num);          /*we get value as 20*/
      printf("Address of num is = %u",&num);  /*we get address as 65524 (Not fixed)*/
      printf("Value of *(&num) is = %u",*(&num)); * we get 20 */
}
Now let's go to intricacies  of our program.
  • value of num is 20
  • &num gives address of memory location where num is present. '&' is called 'address of' and '*' is called value at address. So if we are doing this *(&num), It means we are saying that we want to know the value that is present on address of num. Obviously if we go to address of num we find value num there. So, *(&num) = num. 
Now if we want to store the address of num to a variable itself we can not simply do like int getAddress=&num  because getAddress is not an ordinary variable. It is a variable that contains the address of another variable. So we have to write it as,
int * getAddress = &num;
Also note that getAddress itself is a variable so it should also have address that is generated by compiler. So if we want to hold address of getAddress pointer, all we have to do:
int * * addressOfPointer = &getAddress;



Monday 9 November 2015

Decoding First Program

Let's decode our program and get the meaning of some terms:

Tool Box : As name suggest it contains all the tools to create and display controls on webpage. For example if we want a button  in our web page, all we need to do is drag and drop button control from toolbar to web page. We can open toolbox either by go to VIEW and Select TOOL BOX or type keyboard shortcut CTRL+W,X .

Solution Explorer: It shows organised collection of projects and solution. A solution can contain more than one project.We can open solution explorer either by go to VIEW and Select SOLUTION EXPLORER or type keyboard shortcut CTRL+W,S .

Server Explorer: It is the server management console for Visual Studio. Use this window to open data connections and to log on to servers and explore their databases and system services.We can open server explorer either by go to VIEW and Select SERVER EXPLORER or type keyboard shortcut CTRL+W,L .

Properties Window: It changes property of a web form or control present on web form. For example if we want to change name on a button we go to TEXT property in PROPERTIES WINDOW and Change the text on button. We can open toolbox either by go to VIEW and Select PROPERTIES WINDOW or type keyboard shortcut CTRL+W,P .

Design, Split and Source Mode: Our web form can be viewed in three ways. We can view how our web form look in browser by switch to design view  or if we want to find the source we can select source value. Split is combination of both, so you can view Design and source at the same time. Visual Studio split the window into two parts. In one part it shows design and on other it shows source code. These are very useful when we want to change in our design means when we want to change in html of web form.

Page Directive: A web form  has a CODE BEHIND file and a DESIGNER file. Designer file has .aspx extension where as code behind file has .aspx.cs extension. In code behind file we have to write our code where as designer file contains auto generated code. Web form is associated with its code file using  @Page directive, which is present in html of web form.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Welcome.aspx.cs" Inherits="Welcome" %>





Getting started with First asp.net Program

We are going to create first asp.net program. All you need to download Microsoft Visual Studio(VS) and we are ready to go. I will use VS2010 for creating these programs.

Now, let's go step by step:

1. Open Visual Studio.
2. Go to File => New => Asp.Net Empty Website and then select location and name. Here I set name as AspWebsite.



3. You may delete web.config that is automatically generated. because when we run our program it will be generated automatically.
4. Select solution explorer & right click on solution explorer. Select add new item. Alternatively select solution explorer  and press button ctrl+shift+A. Both will leads to Add New Item.


5. Select Web Form and give it a good name. Here I give Welcome and then click Add. Also note that WebForm has .aspx extension. Also make sure you have selected Visual C# as your programming language from left pane.
6. At this time your page would look like this.


7. We will see details later. Under body tag there is a div tag. Just write your code that we want to display inside browser.
     <div>
        <h2>Wow My First Asp.Net Program!!</h2>
    </div>
8. Run your project by clicking on Start Debugging button or f5.
9. Output would be like this.


Friday 6 November 2015

Introduction to Asp.Net


Well, let's start with Definition first. If you go to MSDN you will find definition as:

ASP.NET is a unified Web development model that includes the services necessary for you to build enterprise-class Web applications with a minimum of coding. ASP.NET is part of the .NET Framework, and when coding ASP.NET applications you have access to classes in the .NET Framework. You can code your applications in any language compatible  with the common language runtime (CLR), including Microsoft Visual Basic and C#. These languages enable you to develop ASP.NET applications that benefit from the common language runtime, type safety, inheritance, and so on.

But the most simple definition is given by wikipedia as:

ASP.NET is an open-source server-side Web application framework designed for Web development to produce dynamic Web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services.

I will discuss various difficult term given in MSDN definition later.

What is webapplication?
A web application is an application that is accessed through a web browser. 
Examples of web browsers include Mozilla,Chrome,Opera,Safari and many more.


Now lets go into detail of web application:
 1- Web application works on client server architecture. 
 2- Client is our browser and server is IIS(Internet Information 
  Services).Every web application runs under microsoft IIS.
 3- Since our browser can only understand HTML and not aspx page so it is the 
  responsibility of IIS to generate HTML from aspx document and send it back. 
 4- Web application needs to be installed on one server and we can access them on every computer 
  with just a browser by specify the URL in browser window.
 5- It can be available over different browsers.
 6- It is accessible from any where if internet is available.


Thursday 5 November 2015

Full ASP.Net & Related Technologies


Hello, My Name is Rajeev Ranjan Tiwary and I am a software Developer. I love to learn and teach new technology.

This Blog name is intentional because I want to cover topics not from Asp.net, Ado.net, C# only but from JavaScript, jQuery, Linq, XML, WCF, SQL also.

I write basic concept only initialy because I am not having in-depth command over Advanced topics till now. Hopefully I will have it soon. :-)

I simply believe its HAVING NO GUIDANCE IS BETTER THAN HAVING BAD ONE!! and I believe on that.That is the reason I first implement concept and then I put it on my blog. I am not a bigot here and you can criticize me for any mistake. But for betterment I expect constructive criticism from you. 

You can connect me via Twitter  and also you can write to me via Mail.
You can also visit my YouTube channel by clicking Here.

You can go to various post by clicking below links:

Server.Execute in Asp.Net


Server.Execute also exposes server side event just like as Response.Redirect. However there are some differences as well.

Now Again we have same problem. We wants to move from Page1.aspx to Page2.aspx on Button click.
Server.Execute is having five overloading versions. I will discuss two of overloaded version here.
  1. Server.Execute(string path); 
  2. Server.Execute(string path,bool preserveForm);
path is given as 
preserveForm ensures that the posted form values can be retrieved means if we post a page from Page 1 to Page 2 and we want to have Page 1 data values to be available on Page 2. All we have to do is send value of preserveForm=true. Well by default it is true so it is kind of optional.

Example:
  1. Server.Execute("~/Page2.aspx");
  2. Server.Execute("~/Page2.aspx",true);
 So all we have to do is drag and drop a button and then we can generate event handler for it by double clicking on the button.

protected void Button1_Click(object sender, EventArgs e)
{
        Server.Execute("~/Page2.aspx", true);
}

Difference Between Server.Transfer & Server.Execute
Server.Transfer terminates the execution of Page 1 (current page) and starts execution of Page 2 (new page), where as Server.Execute process the Page 2 without leaving the Page 1.

Similarity Between Server.Transfer & Server.Execute

  1. Browser Remains on the first page URL.
  2. Both can only be used to navigates to site page on same web server.
  3. Both preserves Form variables from the original request.

Server.Transfer in asp.net



Server.Transfer also exposes server side event just like as Response.Redirect. However there are some differences as well.

Now Again we have same problem. We wants to move from Page1.aspx to Page2.aspx on Button click.
Server.Transfer is having three overloading versions. I will discuss two of overloaded version here.
  1. Server.Transfer(string path); 
  2. Server.Transfer(string path,bool preserveForm);
path is given as 
preserveForm ensures that the posted form values can be retrieved means if we post a page from Page 1 to Page 2 and we want to have Page 1 data values to be available on Page 2. All we have to do is send value of preserveForm=true. Well by default it is true so it is kind of optional.

Example:
  1. Server.Transfer("~/Page2.aspx");
  2. Server.Transfer("~/Page2.aspx",true);
 So all we have to do is drag and drop a button and then we can generate event handler for it by double clicking on the button.

protected void Button1_Click(object sender, EventArgs e)
{
        Server.Transfer("~/Page2.aspx", true);
}

Difference Between Server.Transfer & Response.Redirect

Server.TransferResponse.Redirect
RedirectionRedirection is done by the server.Redirection is done by the browser client.
Browser URLDoes not change.Changes to the redirected target page.
When to useRedirect between pages of the same server.Redirect between pages on different server and domain.

Response.Redirect in Asp.Net

Response.Redirect is very similar to HyperLink control. But the basic difference is HyperLink does not expose any server side event. For example if we want a button and when we click on button, we want to go to Page2.aspx .

protected void Button1_Click(object sender, EventArgs e)
{
        Response.Redirect("~/Page2.aspx");
}

Response.Redirect change the URL in address page and it can be used to navigate pages/website on the same web server or on a different web server.

It has two overloading versions:

  1. Response.Redirect(string Url);
  2. Response.Redirect(string Url,bool endResponse);
Response.Redirect(URL,false) :- Client is redirected to a new page and the current page on the server will keep processing ahead.

Response.Redirect(URL,true) :- Client is redirected to a new page but the processing of the current page is aborted. 

HyperLink Control


This is the most easy options. If you are familiar with HTML and has idea about Anchor tag, it works exactly like that. In fact at runtime hyperlink is rendered as anchor tag.

How to use? 
All you have to do is just drag and drop Hyperlink control. You can find this under standard tab of ToolBox. Now click on HyperLink control and go to properties. All you have to do is change Text and NavigateUrl property.

Text - The link text that will be shown on the webpage as link.

Navigate URL - The URL of the page where we want to go after clicking the link.

ImageURL - The URL of the image, that will be displayed for the link. If we specify both the Text and ImageUrl, the image will be displayed instead of the text. If for some reason, the image is not unavailable, the text will be displayed.

Target - If we set Target to _blank, the web page will be opened in a new window otherwise it will open in same window.

Your source code would look like this:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Page2.aspx" Text="Move to Page 2"></asp:HyperLink>


1- Get started

  1. First Program
  2. Decoding First Program

2 - Go from one webform to other in Asp.Net (Page Navigation Techniques)

There are several techniques to navigate from one page to other. I listed some of them. Click on the techniques and you get detail information about them. These are given as below:

Tuesday 20 October 2015

How to work with WCF?


Windows Communication Foundation (WCF) is a framework for building service-oriented applications.
Let's see how to create a simple service in WCF.

Open Visual Studio. Go to File->New->Project->WCF Service Application

Give name to your application such as WcfService.
Now one service is created by default. Get rid of that because we want to implement our service.
So delete service1.svc and IService1.cs.

[Note: Services have .svc extension]

Now right click on the project name and Add New item(OR ctrl+shift+A ) --> WCF Service.

Give your service name as MyService and click add.
MyService.svc and IMyService.cs is added to your project.
MyService.svc is our Service and IMyService.cs is Interface.


In IMyService.cs we can declare our methods and since MyService.svc inherits from IMyService.cs so we have to implement all methods in our service that is defined in Interface.

By default we have void DoWork() declared in interface and implemented in our service.Delete

   [OperationContract]
   void DoWork();

from IMyService.cs and 

   public void DoWork()
   {
   }

from MyService.svc.cs.

Now add some Method to IMyService.cs as

   [OperationContract]
   string welcome(string name);
   [OperationContract]
   int Sum(int FN,int SN);

Now Go to MyService.svc.cs.

On public class MyService : IMyService line double click on IMyService and press ctrl+.(Dot)
You get 'Implement interface IMyService'. Click on that to generate function signature and change according to following
OR
just paste following in MyService.svc.cs.

        public string welcome(string name)
        {
            return "Hello "+name;
        }

        public int Sum(int FN, int SN)
        {
            return FN + SN;
        }

Now run your project. On left side you see the methods. Click on welcome().
In welcome window give the value of parameter name in Request and Click Invoke Button.

If Security warning appears click on OK and you see your output in Response beneath Request in
Welcome window.

Similarly Click on Sum() and pass value for parameter FN and SN and you get the result.

Now, Let me cover some theory part also.

Contracts: In WCF, All services exposes the contracts. The contract is a platform independent 
and standard way of descibing what the services does.

Service Contract: Describe what operation client can perform on the services.

Data Contract: Defines which data type are passed to and from the service.

Note: Without Service Contract attribute the contact is not visible to WCF clients.

Monday 19 October 2015

Display Digital Clock In Browsers Using JavaScript


<!DOCTYPE html>
<html>
<body>

<p>The Time is :</p>

<p id="display"></p>

<script>
var myVar = setInterval(myTimer, 1000);

function myTimer() {
    var d = new Date();
    document.getElementById("display").innerHTML = d.toLocaleTimeString();
}
</script>

</body>
</html>


Sunday 18 October 2015

Export Gridview to Excel OR Word using Asp.Net

1: Export to Excel

     protected void btnExportGridToExcel_Click(object sender, EventArgs e)
    {
        // It clears all content from the buffer
        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment;filename=Students.xls");
        //Specify the type that you want to change
        //Here we want to change in Excel
        Response.ContentType = "application/excel";
        System.IO.StringWriter stw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(stw);

        gvStudents.RenderControl(htw);
        Response.Write(stw.ToString());
        Response.End();
     }

2: Export to Word Document

    protected void btnExportGridToExcel_Click(object sender, EventArgs e)
     {
        // It clears all content from the buffer
        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment;filename=Students.doc");
        //Specify the type that you want to change
        //Here we want to change in Excel
        Response.ContentType = "application/word";
        System.IO.StringWriter stw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(stw);

        gvStudents.RenderControl(htw);
        Response.Write(stw.ToString());
        Response.End();
     }

Although, When we run project we get error as Control 'gvStudents' of type 'GridView' must be placed inside a form tag with runat=server.

To Remove this error we have to override VerifyRenderingInServerForm() Method.
For this type in Visual studio Override VerifyRenderingInServerform() and then hit ENTER key.We will get:

    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {
        base.VerifyRenderingInServerForm(control);
    }

Since we don't have any requirement of its implementation so just delete                      base.VerifyRenderingInServerForm(control);
from above Method. And Your application will work now. :-)



Export GridView To Excel or Word Document

Monday 21 September 2015

                             SQL Query to write A to Z 

           Without Knowing the ASCII value of any Character



 declare @start int,@stop int     --We declare start and stop variable to hold from and to value
set @start= ASCII('a')              --We get ASCII value of 'a' in start variable
set @stop= ASCII('z')              --We get ASCII value of 'a' in start variable
while(@stop>=@start)             --We start loop. Loop terminate if start value is greater than stop value
begin
print CHAR(@start)
set @start=@start + 1
end





Now If you want to print A to Z means in Capital then we should set 

set @start= ASCII('A') 
set @stop= ASCII('z')  

And rest code will be the same.

Also we can use This code to print all numbers between two numbers.