Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Archive for February, 2010

Delete All Procedures from a database using a Stored procedure in SQL Server

Posted by Viral Sarvaiya on February 17, 2010


In this article I’ll try to explain how you can create a stored procedure that deletes all other stored procedures from a database in Microsoft SQL Server.Some time it is required that you delete all stored procedures from an SQL Server database.

this technique uses the build in sys.objects system table that contains all the objects of current database.

If you filter its selection with a where clause and select only those records that that have type = ‘P’ (this is for procedures) then you can get the names of all the procedures. You can store the list of the procedure names in a temporary table and loop delete the procedures or you can use a cursor. I’ve used a cursor and deleted the procedures one by one using the EXEC function.

Here is the code.


create Procedure DeleteAllProcedures

As

declare @procName varchar(500)

declare cur cursor

for select [name] from sys.objects where type = 'p'

open cur

fetch next from cur into @procName

while @@fetch_status = 0

begin

if @procName <> 'DeleteAllProcedures'

exec('drop procedure ' + @procName)

fetch next from cur into @procName

end

close cur

deallocate cur

Go

hope this will help full to you

Advertisements

Posted in Sql Server | Tagged: , , , , , | Leave a Comment »

ASP.NET – How to call a server-side method from client-side JavaScript

Posted by Viral Sarvaiya on February 12, 2010



Introduction

This is a tutorial on how to call a server-side ASP.NET method from a client-side JavaScript method.

If you are wondering when that could become useful, imagine the following scenario:

A Web Application having an implemented authentication system.
Users log in with their username and password.
At any point the user is able to log out by clicking on the respective “Log Out” button.
On the server-side, the log out action would trigger a cleaning up process of user’s temp data.

However, the user instead of clicking on the “Log Out” button, may simply close the browser window. Now since HTTP is a stateless protocol, the server-side cannot directly detect the user’s action. Therefore the client-side (browser) would have to notify the server that the user is closing the window.

A solution to this problem would be to call a JavaScript function when the client-side “onUnload” event is triggered. The JavaScript function would then be able to call the appropriate server-side method to clean up the data.

The exact required AJAX mechanism to accomplish that kind of communication is described on the “Hello World” project below.

Please note that for the purpose of this tutorial all methods are kept as simple as possible, so that you can easily modify them for your application.

1. Creating a new ASP.NET project

AJAX is required, thus a new “AJAX enabled ASP.NET Web Application” has to be created on Visual Studio 2005 or “ASP.NET Web Application” on  2008.

aspnet_server_side_from_client_side_1

2. Modifying the server-side code

Every server-side method that is called from the client-side, must be declared as “static”, and also has to be decorated with the [System.Web.Services.WebMethod] tag.

Now let’s create a simple function that returns a string value.


[System.Web.Services.WebMethod].
public static string Message()
{
 return "Hello from the server-side World!";
}

3. Modifying the ScriptManager

The “EnablePageMethods” attribute has to be added on the ScriptManager tag.


<asp:ScriptManager ID="ScriptManager1"  runat="server"EnablePageMethods="true"  />

4. Adding a simple HTML button

We are going to add a simple HTML button rather than a server-side ASP.NET button control. The “onClick” event is going to be associated with the JavaScript function “GetMessage”.


<input onclick="GetMessage()"  type="submit" value="Get Message" />

5. Adding the JavaScript code

Let’s add the “GetMessage” JavaScript function, which is going to call our server-side “Message” method.


function GetMessage() {
PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
}

The “OnGetMessageSuccess” is the name of the JavaScript function that will be called if the request is successful. Whereas the “OnGetMessageFailure” will be called if an exception is thrown.

So let’s add these two functions:


function OnGetMessageSuccess(result, userContext, methodName) {
alert(result);
}

function OnGetMessageFailure(error, userContext, methodName) {
alert(error.get_message());
}

Please note that you can give to the functions any name you wish, as long as they match the PageMethods call parameters.

If there are no errors, the “OnGetMessageSuccess” will show a pop-up window with our server-side “Message” text. Else, the pop-up will have an exception message.

6. Running the Web Application

This is it, we are ready to run our Web Application. Everything seems to be working just fine on Internet Explorer (IE6 and IE7):

aspnet_server_side_from_client_side_2

However if we run it on Firefox (currently the latest version is 3.0.4) the pop-up will display the following message:

The server method ‘Message’ failed.

aspnet_server_side_from_client_side_3

7. Fixing the Firefox issue

We just need to modify the button’s onclick event a bit:


<input type="submit"  value="Get Message" onclick="GetMessage();return false;" />

And this would do the trick:

aspnet_server_side_from_client_side_4

8. Here is the complete source code for your reference

Default.aspx

(Had to replace double quotes (“) with single quote (‘) in order to post it correctly.)


<%@ Page Language='C#' AutoEventWireup='true' CodeBehind='Default.aspx.cs' Inherits='AJAXEnabledWebApplication2._Default' %>

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>

 <html xmlns='http://www.w3.org/1999/xhtml' >
 <head runat='server'>
 <title>Page</title>
 <script type='text/javascript'>
 function GetMessage() {
 PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
 }
 function OnGetMessageSuccess(result, userContext, methodName) {
 alert(result);
 }
 function OnGetMessageFailure(error, userContext, methodName) {
 alert(error.get_message());
 }
 </script>
 </head>
 <body>
 <form id='form1' runat='server'>
 <asp:ScriptManager ID='ScriptManager1' runat='server'EnablePageMethods='true' />
 <div>
 <input type='submit' value='Get Message'onclick='GetMessage();return false;' /></div>
 </form>
 </body>
 </html>

Default.aspx.cs


 using System;
 using System.Data;
 using System.Configuration;
 using System.Collections;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;

 namespace AJAXEnabledWebApplication2
 {
 public partial class _Default : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {

 }
 [System.Web.Services.WebMethod]
 public static string Message()
 {
 return "Hello from the server-side World!";
 }
 }
 }

Posted in ASP.NET | Tagged: , , | Leave a Comment »

Useful RUN commands

Posted by Viral Sarvaiya on February 10, 2010


Here are some of the commands that can be executed from the Run dialog box in the Start menu to launch the following applications. I have picked up them over the years and am not sure of one particular source as a reference.

  • MSConfig – Microsoft configuration utility
  • Explorer – Windows explorer
  • Cmd – Command window
  • Inetmgr – IIS Manager
  • Services.msc – Windows services Administrator
  • Pbrush – Paint brush
  • Chrome – Google chrome browser.
  • Firefox – Firefox browser
  • ISqlw – Query analyzer
  • Eventvwr.msc – Event viewer
  • Dcomcnfg – Component services
  • Notepad – Notepad
  • Calc – Calculator
  • Winword – MS Word
  • Excel – MS Excel
  • Access – MS Access
  • Regedit – Registry editor
  • Taskmgr – task manager
  • Write – Wordpad
  • Osk – On screen keyboard
  • Logoff – logs of the user
  • Shutdown /s – shutdowns the system
  • Perfmon – Performance monitor
  • Mstsc – Remote desktop
  • Firewall.cpl – Windows firewall
  • odbcad32 – ODBC administrator

Posted in General | Tagged: , , | Leave a Comment »

Windows Keyboard Shortcuts

Posted by Viral Sarvaiya on February 10, 2010


Following are some keyboard shortcuts that use very often.

  • Ctrl + Enter – Auto complete a URL, prepends and appends a website name with “http://” and “.com”.
  • Shift + Enter – Auto completes a URL, prepends and appends a website name with “http://” and “.net”.
  • Ctrl + shift + Enter – Auto completes a URL, prepends and appends a website name with “http://” and “.org”.
  • Ctrl + Esc – Opens the Start menu.
  • Ctrl + Tab – Switch between open tabs in a MDI.
  • Ctrl + Shift + Tab – Switch between open tabs in a MDI in reverse order.
  • Ctrl + Shift + Esc – Opens windows task manager.
  • Ctrl + A – Make a selection.
  • Ctrl + T – Open a new tab in IE, Firefox, Chrome.
  • Ctrl + W – Close a tab in IE, Firefox, Chrome.
  • Ctrl + X – Cut
  • Ctrl + C – Copy
  • Ctrl + V – Paste
  • Ctrl + Z – undo
  • Ctrl + R – Redp
  • Ctrl + B – Bold
  • Ctrl + S – Save the file
  • Ctrl + U – Underline a selection
  • Ctrl + F – Find
  • Ctrl + H – Search and Replace
  • Ctrl + G – Go to line #
  • Ctrl + O – Opens file dialog box
  • Ctrl + P – Opens print manager
  • Ctrl + D – Bookmark a web page in a browser
  • Alt + Tab – Switch between open programs
  • Alt + Shift + Tab – Switch between open programs in reverse order.
  • Alt + Enter – Display properties (file, folder, etc).
  • Alt + F4 – Close a window.
  • Alt + Space – Display system menu of active window.
  • Alt + Home – Launch home page in a browser application.
  • F1 – open help file of the current application.
  • F2 – rename a file.
  • F5 – Refreshes the current window
  • Shift + F10 – Display context menu
  • Windows logo + E – open explorer
  • Windows logo + M – Minimize all windows
  • Ctrl + Alt + End – Security dialog in remote desktop
  • Ctrl + Alt + Break – Toggle full screen in remote desktop
  • Alt + Home – Display start menu

Posted in General | Tagged: , , , | 1 Comment »

Cascading DropDownLists With ASP.NET and jQuery

Posted by Viral Sarvaiya on February 8, 2010


Cascading drop down lists is a really nice feature for web developers. I thought it was time to write an article on how to do this using ASP.NET and jQuery.

for more information :

http://www.dotnetcurry.com/(X(1)S(3rsl2155hsgai545ysu1liio))/ShowArticle.aspx?ID=417&AspxAutoDetectCookieSupport=1

Posted in ASP.NET | Leave a Comment »

 
%d bloggers like this: