Thursday, September 29, 2011

Disable button in asp net web page to prevent multiple clicks


When a user clicks a button and the response is slow, there are chances that user may click the button again. The scenario may occur both when the button postbacks synchronously or asynchronously. This type of multiple clicks could be prevented if we could just disable the button just after the first click and enable it again when the processing is done. This is quite easy to implement the task in both the cases: synchronous and asynchronous postbacks.
Before we jump on the topic, you may learn how to click a button on enter key press in an asp.net textbox control. Similarly you may be interested in displaying google search-like watermark in an asp.net textbox control. Both the tutorials help you work with asp.net button control more interactively.
Fig. 1: Just after clicking asp.net button

Fig. 2: After postback occurs

If you are using ajax processing on button click, just disable the button when it is clicked and enable it when the ajax processing is done. If you are using full postback on button click, you may just make the button invisible using client-side-scripting when the click occurs. And you don't need to worry about making it visible since after postback the page will be rendered again, with the button visible as usual. In the snippet below I have shown how to 'disable' (in fact disabling won't work since the server side event is not firing) the asp.net button from client side when it is clicked. 
1. Add reference to jquery file

2. Design the web form
   
                           
   
   
3. Write the client scripting function with jquery

4. The code-behind.
protected void Button1_Click(object sender, EventArgs e)
    {
        lblMsg.Text +="Current time : "+ DateTime.Now.ToLongTimeString()+"
";
    }

That's all. When you click the button you catch the client side click event and make the button just invisible. Meantime show 'processing' or similar message. If you love ajax loading image, generate one and show it. From codebehind I have just displayed current time in a label.
Happy Bijaya Dashami (Dashain - the greatest festival of Hindus)!
kick it on DotNetKicks.com

Monday, September 26, 2011

Exclusive access could not be obtained because database is in use-sql server restore database error

While publishing a website to the web server, I took backup of my database. Now I had abc.backup at my hand. Next step was to create a database in the sql server of my host. I did it. Then I uploaded the backup file to the server. When I tried to restore the database I got following error:
Exclusive access could not be obtained because the database is in use
This error normally discourages because we wonder what exclusive access is this that we need to successfully restore the database. After searching a while you come to conclusion that the database to which we are restoring our backup is in use by one or some users you have already added. The popular solution is - temporarily isolate the user from the database Easy, if you run this command at your server:
Use master
go

Alter Database mydbname
SET SINGLE_USER With ROLLBACK IMMEDIATE

 RESTORE DATABASE mydbname
 FROM DISK = 'C:\abc.bak'
Now you are right if you suspect whether we should the roll back to multi user. Yes this way:
Use master;
go

ALTER DATABASE mydbname

SET MULTI_USER;
go
Note that we run the commands against the master database, not the candidate database itself. And did I do the same last time? Nope. I was just happening with a slight mistake. I had created users for the db before I restored the database. So to save yourself from all those stuffs explained above, just create the database in the server, restore your backup file to it and only then create the database users. Done! Thanks.kick it on DotNetKicks.com

Thursday, September 22, 2011

There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined

Last time I got the following error:
HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.
See in the image below the following config error, and also watch the config source.
There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined
Root of the error
I got the error when I was deploying a web application precompiled in asp.net 2.0 onto the production server with asp.net 4.0. The simple search reveals that it is known issues with VS 2010 and asp.net 2.0. But my site is asp.net 2.0 enabled. What's the root of the error then?  
My conclusion (and please correct me if necessary) I have configured asp.net web extension namespaces in my web.config file. You don't see this in asp.net 3.5 and 4.0 enabled sites since servers with asp,net 3.5/4.0 by default server asp.net ajax services. But asp.net 2.0 has to be ajax enabled by installing asp.net ajax extension 1.0 which you can download from here. Sensed duplicate entry of system.web.extension section group? Yes, don't include the following section group in the config section if your site runs on asp.net 2.0 but published on asp.net 3.5/4.0.
    

    
      
        
Comment out the config section (or remove it if you like), and you are done. Good luck! kick it on DotNetKicks.com

Friday, September 16, 2011

How to add favicon in your asp-net site?

Make your site impressive and standard by displaying favicon. This is a practice of branding also. See in the following image the red circled area at the top corner. The black filled square is the favicon of dotnetspidor: A dot net programming blog. You can see the favicons in almost all of the popular websites.
So how to achieve this? First create a favicon image of your own. Visualize a great logo/image for the favicon. Save it as favicon.ico. Drop it at the root folder of your site. Now you are one step away - just put the following two links just below the title of header of your form. If you are using master page, just put these lines in the master page and you are done for all the pages.

    
    
        

So why the two lines? Normally the first link works. But for some tricky and picky browsers the second link will work. All the best! kick it on DotNetKicks.com

Saturday, September 3, 2011

Delete all stored procedures of a database in sql server

Sometimes there arises the need of deleting all stored procedures of a database. I came across the same situation when I was generating CRUD operations stored procedures via third party tool. I once changed a table and then had to regenerate all the stored procedures. So went on to find out the way. And ultimately stumbled at http://ctrlf5.net/?p=164. Here is how to delete all the stored procedures of a sql server database:
USE myDBName
GO
DECLARE @procedureName varchar(500)
DECLARE cur CURSOR
      FOR SELECT [name] FROM sys.objects WHERE type = 'p'
      OPEN cur

      FETCH NEXT FROM cur INTO @procedureName
      WHILE @@fetch_status = 0
      BEGIN
            EXEC('DROP PROCEDURE ' + @procedureName)
            FETCH NEXT FROM cur INTO @procedureName
      END
      CLOSE cur
      DEALLOCATE cur
All the best! kick it on DotNetKicks.com

Popular Posts

Recent Articles