Saturday, 28 March 2015

Params Keyword in C#

By using the params keyword, you can specify a method parameter that takes a variable number of arguments.
You can send a comma-separated list of arguments of the type specified in the parameter declaration or an array of arguments of the specified type. You also can send no arguments. If you send no arguments, the length of theparams list is zero.
No additional parameters are permitted after the params keyword in a method declaration, and only one paramskeyword is permitted in a method declaration.

Example:-
Write the below code in a console application.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Program
{
 static void Main(string[] args)
 {
  int y = Add(10, 20);
  Console.WriteLine(y);
  Console.ReadLine();
 }
 public static int Add(params int[] MyColl)
 {
  int total = 0;
  foreach (int i in MyColl)
  {
   total += i;
  }
   return total;
  }
}
When we run this code we get the following output.
ss3

Tuesday, 24 March 2015

Cross Page Postback in ASP.NET

In this article we will see how to use Cross page postback in ASP.NET.

When there is a need to transfer some data from one web page to another web page then normally the first thing that comes in a developer’s mind are the sessions. But using session can be bad sometimes as the page gets heavy because of this. There is one more way to achieve this and thereby avoiding sessions or other state management techniques, we can use a cross page postback. It simply transfers the data from one page to another. In this article we will create a sample application to check the same.

Let us see a sample demo for the same.

Open Visual Studio, Create a new web application. Name it “CrossPagePostbackDemo”.

Add a Web Form to the project. Name it “Index.aspx”. Add another web form to the project. Name it “Display.aspx”.

Add the following HTML code in the “Index.aspx” web form.

<table>
 <tr>
  <td>
    Name:
  </td>
  <td>
    <asp:TextBox ID="txtName" runat="server" />
  </td>
</tr>
<tr>
 <td colspan="2">
  <asp:Button ID="BtnSubmit" Text="Move to next Page" runat="server" PostBackUrl="~/Display.aspx" />
 </td>
 </tr>
</table>
In the second web form “Display.aspx” write the following HTML.


<div>
 <asp:Label ID="LblName" runat="server"></asp:Label>
</div>


 
So basically in our first form we have a textbox that accepts name. When we click the button the textbox value should get transferred to the Display page. To achieve this we need to write a bit of code in the “Display.aspx.cs”.
Write the following code in the “Display.aspx.cs”.
protected void Page_Load(object sender, EventArgs e)
 {
  if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
   {
    TextBox txtName = (TextBox) PreviousPage.FindControl("txtName");
    LblName.Text = "Welcome " + txtName.Text;
   }
  else
   {
    Response.Redirect("Index.aspx");
   }
 }
As we can see in the page load of this file we have two conditions. The first condition checks that there exists a previous page from where the request is coming. In other words it is used to prevent a user directly opening this page. If the user does this then the condition returns false.

The second condition checks whether really a cross page postback has occurred.

Set Index.aspx as the starting page in the application. Run the Page.

Enter some text and hit the button.

c1

Once the button is clicked we are redirected to next page. Refer below image.

c2

As expected we have the Name transferred from Index page to the Display page. So without using sessions we have achieved this scenario.

Now we will try to browse the “Display.aspx” page directly just to make sure we do not get to see the same output.

 So just browse the second page directly and see the output.

c3

We are redirected to the first page as per the condition defined in the else condition in the page load
 

Handle Errors in SQL Server

In this blog we will see how to handle errors in SQL server.
We use try catch blocks just like C#. In this example we will try to divide a number by Zero and let us see how the error message is displayed. When the error comes it goes to catch block.
Write the below script in SQL Server
BEGIN TRY
DECLARE @number INT
SET @number = 2/0
PRINT 'This will not execute'
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber,
          ERROR_SEVERITY() AS ErrorSeverity,
          ERROR_STATE() AS ErrorState,
          ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO
When we run this script we get the following output.

 


In this Article we will see how to insert values in a table with just an identity column.


When we make an identity column in a table then for every record inserted in the table the identity column auto increments the value without specifying the identity column in the Insert command. What if we have to insert record in this column manually using a SQL command? So in this article we will just see how to achieve this.
We will create a table with just an identity column. Once the table is created we will try to insert records in this table using SQL query.
Write the following script in SQL Server to create a table.


Create table tblInsertintoIdentityColumn
(
Id int primary key identity
)



Execute the above SQL code to create a table in the selected database. Execute the following query to check the contents in the table. As we haven’t inserted any records in this table, so the query will just return the table’s schema.


Select * from tblInsertintoIdentityColumn


s1
Now we will insert value in this table. Write the following query.

Insert into tblInsertintoIdentityColumn default values

Execute the preceding SQL command.

s2
As we can see the command is successfully executed. So let us check the records in the table.

Run a select query and check the records in the table.

s3
And so on we can insert as many records in the Identity column in a table.