Tuesday, June 18, 2013

Trigger in Sql Server

Trigger is a logic which executes on the tables after or before when the insertion, deletion and updation query executes. 

There are two type of trigger
  1. Instead of trigger
  2. After Trigger

Instead of trigger : - It fire before the insert/Update/Delete executes on table.

After Trigger : It fires After insert/delete/update command executes on table.

 Lets Start Understanding the use of trigger in sql server -
First we Create trigger : - this one is after trigger

 
CREATE Trigger tblUpdate   
On tblemp   
After Insert,Delete, Update   
As Begin   
   
Declare @Oldvalues nvarchar(50)   
Declare @newvalues nvarchar(50)   

   
--Fetch inserted value   
Select @newvalues=Customername from inserted   
--fetch deleted value   
   
Select @Oldvalues=Customername from deleted   
   
insert into tblaudit(LastUpdated,Oldvalues,newvalues)   
values(getdate(),@Oldvalues,@newvalues)   
   
End   


Description of Used word in trigger : -
tblUpdate - Trigger Name
tblemp     - Table name on which trigger will fire.
Inserted  - When any insert command executes it will store the current value.

Deleted   - When any delete command excutes it will store the deleted data.


Here i have created two table one is tblemp and tblaudit
Table tblaudit


CREATE TABLE [dbo].[tblaudit](
    [LastUpdated] [datetime] NULL,
    [Oldvalues] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [newvalues] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]


Table tblemp

CREATE TABLE [dbo].[tblemp](
    [CustomerID] [int] IDENTITY(1,1) NOT NULL,
    [Customername] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]


When any insert update delete command executes trigger will fire automatically..

Note : First create table then trigger.
Watch Video click here

Monday, June 17, 2013

405 - HTTP verb used to access this page is not allowed.

The HTTP verb POST used to access path '/html5demos/post.html' is not allowed.

When you tried to work on html page in Visual studio sometime it fire above error.

<!DOCTYPE html>
<html>
<head>
    <title>Untitled Page</title>
</head>
<body>
    <form name="form" id="form1" method="post" action="post.html" target=_self>
    <input type="text" id="text2" name="text2"  />
    <input type="text" id="Text1"  />
    <input type="submit" name="Submit" value="Submit" />
  
    </form>
</body>
</html>

Solution

Problem : File type is not registred in IIS amp setting. Http POST , HEAD are respond with 405 error.

1.GO to Control Panel "Administrative Tools" --> IIS
2.Select The Website and select handler mapping
3.Add Script map
Request Path Should Be " .Htm "
or ".html"
Select this Path  C:\Windows\System32\inetsrv\asp.dll

Name  what ever u can 

After this click on Request Restriction 
 Mapping  :  "Invoke handler only if request is mapped to :" 
check "file" 

Verbs :  "All Verbs"

In the last Click Okay OK

Hope it will work fine... 


Friday, June 14, 2013

Saturday, June 1, 2013

Upload Image through FCK Editor Asp.net

Upload Image through FCK Editor Asp.net it always throws the some issue :-
When trying to upload images through FCK Editor tool inside an aspx page it throws an  error[XML 403 path Error].
To Fix the issue we have to configure manually.

STEP-I :
File Path  :  fckeditor/fckconfig.js
Previously :
var _FileBrowserLanguage    = 'asp' ;  // asp | aspx | cfm | lasso | perl | php | py
var _QuickUploadLanguage    = 'asp' ;  // asp | aspx | cfm | lasso | perl | php | py

We have to change 'asp' to 'aspx'

STEP-II
File Path  :  fckeditor\editor\filemanager\connectors\aspx\config.ascx
Previously :
           Change-1 : Enabled = CheckAuthentication();
                       
                          Change-2 : UserFilesPath = "~/upload/";
           Change-3 : UserFilesAbsolutePath = "C:\\upload\\";
                            
Chnage -1 : change it to Enable = true;
Change -2 : change the folder name in which you want to store the uploaded image.
Change -3 : change the absolute path of the folder in which you want to store the uploaded image.

Best Code practice C#

Use int.TryParse( ) to keep away exception .
Convert.Int32 throws an exception of "System.FormatException" when the input parameter is wrong .

Example

If we are converting a QueryString value to integer and the QueryString value is "de8913jhjkdas", then -
int employeeID = 0;
employeeAge = Convert.ToInt32(Request.QueryString.Get("Userid")); //Here it will throw an Exception.

int employeeAge = 0;
int.TryParse(Request.QueryString.Get("Userid");, out employeeAge); //Here it will not throw any Exception.

You can also use bool.TryParse, float.TryParse, double.TryParse, etc for converting into other datatypes.



Convert.ToString() V/S obj.ToString()



Use Convert.ToString(), because if you are using obj.ToString() and the object(obj) value is null it will throw an excetion of type "System.NullReferenceException".
The cause of the exception is null doesn't have a method called ToString().
Response.Write("Name : " + Session["User_Name"].ToString());        //Here it will throw an Exception if Session["User_Name"] is null.
Response.Write("Name : " + Convert.ToString(Session["User_Name"])); //Here it will not throw any Exception.
 String.Empty V/S ""


If you want to check whether a string is empty or not then use String.Empty instead of "", because "" will create a new object in the memory for the checking while String.Empty will not create any object, because Empty is a read-only property of String class.
So its better to use if("Devi" == String.Empty) instead of if("Devi" == "").
You can also use if("Devi".Length == 0) for doing the same thing but if the string is null then it will throw an exception of type "System.NullReferenceException".
 
 String.IsNullorEmpry() V/S ("" || null)


Suppose you want to check a string for both its emptiness and nullability then its better to use String.IsNullOrEmpty() instead of ("" || null). Because "" will create a new object but String.IsNullOrEmpty() will not do so.
So its better to use if(String.IsNullOrEmpty("Devi")) instead of if("Devi" == "" || "Devi" == null)