public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Archive for January, 2009

Parse and TryParse

Posted by Virendra Dugar on January 30, 2009

Parse method is used to parse any value to specified data type.

For example
string test = “42”;
int32 Result;
Result = Int32.Parse(test);

This will work fine but what if when you are not sure about the value of string variable test. As for example if test=”abc” and if you try to use above method, .NET will throw an exception as you are trying to convert string data to integer.

TryParse is a good method if the string you are converting to an interger is not always numeric.

if(!Int32.TryParse(test,out iResult))
{
//do something
}

TryParse method returns a boolean to denote whether the conversion has been successfull or not, and returns the converted value through an out parameter.

**declare variable iResult of Int32.

Enjoy….

Advertisements

Posted in C# | Tagged: | 1 Comment »

Java script function to get browser name.

Posted by Virendra Dugar on January 28, 2009

We have this requirement in most of the application to know that which browser user is using to view the web site. We can use javascript to know browser name and this function works with all the broswers…

function GetBrowser()
{
           if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
           { 
                 var ffversion=new Number(RegExp.$1) 
                 if (ffversion>=3)
                  alert(“FF 3.x or above”)
                 else if (ffversion>=2)
                  alert(“FF 2.x”)
                 else if (ffversion>=1)
                  alert(“FF 1.x”)
            }
            else if(/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent))
            {
                 alert(“Chrome.”);
            }

           else if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent))
            {
                 alert(“Opera”);
            }
            else
            {
             alert(navigator.appName);
            }
             
            return false;
}

 window.onload  = GetBrowser;

Enjoy…………..

Posted in JavaScript | Tagged: | Leave a Comment »

Disabling button at client side before firing server side event

Posted by Virendra Dugar on January 27, 2009

If we want to disable the button when clicked until it’s execute the server side code then check out this link….

Enjoy…

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

ASP.NET 2.0 Features: app_offline.htm

Posted by Virendra Dugar on January 27, 2009

There is a simple way to bring down your ASP.NET 2.0 application. The only thing you have to do is to create simple html file called App_offline.htm and deploy it to your ASP.NET 2.0 web application root directory. The rest is handled by ASP.NET runtime internal routines.

The way app_offline.htm works is that you put the file in the root directory of the application. When ASP.NET sees it, it will shut-down the app-domain for the application (and not restart it for requests) and instead send back the contents of the App_offline.htm file in response to all new dynamic requests for the application. When you are done updating the site, just delete the file and it will come back online.

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

Alternate text for images for different browsers

Posted by Virendra Dugar on January 23, 2009

To show any image on the webpage either img tag is used or <asp:Image> tag is used. We also put some alternate text for the image which is displayed when mouse is over the image.  It works as a tool tip.

To show alternate text on the image we use
alt” attribute for img tag .
Alternatetext” attribute for asp:Image.

But the funny side of these attributes is they are not supported by Firefox. If you use any of these attribute to show alternate text then alternate text is not displayed in Firefox.

After some hard work I found a common solution which is supported by most browsers. Solution is to use ” title” attribute instead of “alt” attribute. This attribute is supported by all the browsers.
<img src=”sunset.jpg” title=”Image1″> </img>

So if you are making browser independent application, stop using alt attribute and switch to title attribute.

Enjoy.

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

Send mail From SQL server

Posted by Virendra Dugar on January 22, 2009

It is a very common situation when it is required to send mails. There are so many ways to send mail. 

One way is to send mail through Sql server 2005.

There are two methods available
1. DataBase Mail
2. Thorugh OLE

Here is the Stored Procedure to send mail through OLE.

Just copy this prodedure and run it in your database…

–WSA_sp_SendEmail ‘virendra@mailserver.local’,’virendra@mailserver.local’,’Hi’,’Test Mail From CDO’
CREATE PROCEDURE [dbo].[WSA_sp_SendEmail]
@From varchar(100),
@To varchar(1000),
@Subject varchar(250),
@Body varchar(max),
@CC varchar(1000) = null
AS
BEGIN
Declare @iMsg int
Declare @hr int
Declare @source varchar(255)
Declare @description varchar(500)
Declare @output varchar(1000)
EXEC @hr = sp_OACreate ‘CDO.Message’, @iMsg OUT
Print @From
EXEC @hr = sp_OASetProperty @iMsg, ‘Configuration.fields(“http://schemas.microsoft.com/cdo/configuration/sendusing&#8221;).Value’,’2′
EXEC @hr = sp_OASetProperty @iMsg, ‘Configuration.fields(“http://schemas.microsoft.com/cdo/configuration/smtpserver&#8221;).Value’,10.0.0.100
EXEC @hr = sp_OAMethod @iMsg, ‘Configuration.Fields.Update’, null
–Print @To
EXEC @hr = sp_OASetProperty @iMsg, ‘To’, @To
EXEC @hr = sp_OASetProperty @iMsg, ‘From’, @From
if (@Cc <> ”)
EXEC @hr = sp_OASetProperty @iMsg, ‘Cc’, @Cc
EXEC @hr = sp_OASetProperty @iMsg, ‘Subject’, @Subject
EXEC @hr = sp_OASetProperty @iMsg, ‘HtmlBody’, @Body
EXEC @hr = sp_OAMethod @iMsg, ‘Send’, NULL
IF @hr <>0
select @hr
–print @hr
BEGIN
EXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUT
Print @hr
IF @hr = 0
BEGIN
SELECT @output = ‘ Source: ‘ + @source
PRINT ‘hi’
PRINT @output
SELECT @output = ‘ Description: ‘ + @description
PRINT @output
END
ELSE
BEGIN
PRINT ‘ sp_OAGetErrorInfo failed.’
RETURN
END
END
EXEC @hr = sp_OADestroy @iMsg
END

*Please change the SMTP server address in the Procedure. Currently it is set to 10.0.0.100, Change this address according to your SMTP server Address.

To send the mail one have to “Enable OLE Automation”.

Go to Sql server source configuration area and enable OLE Automation from the options list.

Sql server Source Configuration area is under Start->Microsoft Sql Server-> Configuaration Tools..

Enjoy….

Posted in SQL Server | Tagged: | Leave a Comment »

How to display your own logo in the address bar?

Posted by Virendra Dugar on January 19, 2009

If you need to put your website logo in the top address bar instead of browser’s logo then follow these steps.
1. Add a file called “favicon.ico” (an icon file of your website logo) to your website root directory.
2.Insert the following HTML tag inside the <head> … </head> section of your web page.
<link rel=”shortcut icon” href=”favicon.ico” >

**If you are working with master pages then put this code in head section of master page otherwise you have to put this code in every page.

Enjoy…

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

Assign Value to Asp.Net TextBox with mode=”Password”

Posted by Virendra Dugar on January 16, 2009

Quick summary of what’s changed in ASP.NET Core 1.0

ASP.NET server control doesn’t want to add/display the value in a password textbox because of security concerns. You have to add it yourself using an attribute.

Add this following line of code when you want set the value 

TextBox1.Attributes.Add(“value”, “thePassword”)
*( TextBox1 is a password textbox.)

While given solution will work, if a user does a view source on the form, the password is visible as plain text and that is never good from a security standpoint.

Rather, I would use Viewstate to hold the password value and if the userdoesn’t change it then retrieve the value from view state.

Other Scenario is:-

On most of the web sites at the time of registration password is asked.You must have noticed after submitting the page if you are again redirected to the page due to unsuccessful registration value of password field is lost.

This is due to security concerns as everything is gets rendered on the client side So when you view the source in the browser password value will be there.

But if you want to set the value of password text box explicitly then use following method in .NET 2.0

txtPassword.Attributes[“value”] = Request.Form[txtPassword.ClientID]; //in C#.Net 2.0

Enjoy…..

Learn new ASP.NET Core 1.0 Framework

Posted in ASP.NET | Tagged: | 11 Comments »

TOP WITH TIES clause in SELECT queries

Posted by Virendra Dugar on January 12, 2009

The SELECT TOP N query always returns exactly N records, and randomly drops any record that have the same value as the last record in the group.

SELECT TOP 5 price, Booktitle FROM BookTitles ORDER BY price DESC

This query will give 5 records from table BookTitles in descending order by price. Problem over here is Suppose the last book title has a price tag of $19.99. The BookTitles table contains two more books with the same price, but they are ignored by the TOP clause.

To see those recrods add the WITH TIES clause…

SELECT TOP 5 WITH TIES price, Booktitle FROM BookTitles ORDER BY price DESC

**WITH TIES will only work with Order by Clause.

Posted in SQL Server | Tagged: | Leave a Comment »

Asp.Net 2.0 File Upload Control

Posted by Virendra Dugar on January 8, 2009

ASP.Net 2.0 comes up with a completely new File Upload Control. This is how you can declare the file upload control.
<asp:FileUpload id=”FileUpload1″ runat=”server” />

Here are some useful properties:-
FileUpload1.FileName :- Will give File Name with the full Path.
FileUpload1.PostedFile.FileName :- Will give only the file name.
FileUpload1.PostedFile.ContentLength :- will give size of the file in Bytes.
FileUpload1.PostedFile.ContentType :- will give MIME type of uploaded file, i.e. “image/gif”.

By Default, maximum allowed size for file is 4MB. To allow files larger than the default of 4MB, one need to change the web.config.There is a parameter called maxRequestLength which takes value in the KB.

<system.web>
<httpRuntime executionTimeout=”1000″ maxRequestLength=”1048576″/>
</system.web>

The maxRequestLength property dictates the size of the request made to the Web server.When you upload files, the file is included in the request.

This example changes the maxRequestLength property’s value to 1048576KB (around 1GB). With this setting in place, end users can upload 1Gb file to the server.

There is a one more property named “executionTimeout”.This property sets the time (in seconds) for a request to attempt to execute to the server before ASP.NET shuts down the request (whether or not it is finished). The default setting is 90 seconds.
The end user receives a timeout error notification in the browser if the time limit is exceeded. If you are going to permit larger requests, remember that they take longer to execute than smaller ones. If you increase the size of the maxRequestLength property, you should examine whether to increase the executionTimeout property as well.

Uploading files in ASP.NET is very inefficient.When you pick a file and submit your form, IIS needs to suck it all in and only then you have access to the properties of uploaded file(s).You can not do about the fact that you have to sit through a long upload and wait. Neither can you display a meaningful progress bar because there’s no way to know how much is transmitted at any given time.Once IIS buffers your upload, ASP.NET takes it from there.

You might receive errors when your end users upload files to your Web server through the FileUpload control in your application. These might occur because the destination folder on the server is not writable for the account used by ASP.NET. If ASP.NET is not enabled to write to the folder you want, you can enable it using the folder’s properties.

 

**File upload contol also works within the update panel but Postback trigger needs to be used. 


File upload within Update Panel

As you can see from the image, I have placed the file upload control in Update panel and I have used post back trigger to upload the file.

Although there are some third party controls are also available which are very handy. Check this link. This control also shows the progress while uploading the file. 

Enjoy…

Posted in ASP.NET | Tagged: | 3 Comments »

 
%d bloggers like this: