Sunday, February 26, 2012

How to add URL in favourites using ASP.NET and JavaScript

One another regular question in forum. This is a simple code which helps you to add URL in favourites. Here I have used both JavaScript and ASP.NET code.

I am giving two sample code here.

Method 1:

First we will add the current URL by opening Add URL pop up and allow the user to enter the favourites URL title.

//btnAddFavourites is the button name 
btnAddFavourites.Attributes.Add("OnClick", "window.external.AddFavorite(location.href, document.title); return true;");

Below is the pop up window sample,

image

Method 2:

In this method we will not take any input from user. We will add a simple JavaScript function and hard code both the URL and Title.

Add below JavaScrit just below your <head> opening tag,

<script language="javascript">
        function AddToFav()
        {
        bookmarkurl= "http://asheej.blogspot.com/";
        bookmarktitle="Good .NET Articles"
        if (document.all)
        window.external.AddFavorite(bookmarkurl,bookmarktitle)
        else if (window.sidebar) // firefox
        window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
        }
    </script>

Now we will call this function on button click,

//btnAddFavourites is the button name
btnAddFavourites.Attributes.Add("OnClick", "AddToFav(); return true;");

Just try this in your website.

Cheers Asheej!

Saturday, February 25, 2012

How to get max value from a varchar field in MS SQL

Here I am going to explain you how to get max value from a varchar field in MS SQL.

Below is the table we are going to use here,

CREATE TABLE [Customer]
(
    [CustomerID] [nvarchar](50) NOT NULL,
    [CustomerName] [nchar](10) NULL,
    [Address] [nvarchar](50) NULL,
  CONSTRAINT [pk_Cust_ID] PRIMARY KEY CLUSTERED 
  (
    [CustomerID] 
  )
)
GO

If you look at the table I have declared CustomerID as nvarchar data type which means we will have both numeric and alphanumeric values.

Now we will insert some sample data,

INSERT INTO [Customer] ([CustomerID] ,[CustomerName],[Address]) VALUES ('1','Name1','Address1')
INSERT INTO [Customer] ([CustomerID] ,[CustomerName],[Address]) VALUES ('2','Name2','Address2')
INSERT INTO [Customer] ([CustomerID] ,[CustomerName],[Address]) VALUES ('11','Name11','Address11')
INSERT INTO [Customer] ([CustomerID] ,[CustomerName],[Address]) VALUES ('2a','Name2a','Address2a')
INSERT INTO [Customer] ([CustomerID] ,[CustomerName],[Address]) VALUES ('1a','Name1a','Address1a')
INSERT INTO [Customer] ([CustomerID] ,[CustomerName],[Address]) VALUES ('21','Name21','Address21')
GO

In the above statement 21 is the highest value.

SELECT MAX(CAST(CustomerID AS Int)) as [Customer ID] FROM Customers
 
WHERE ISNUMERIC(CustomerID)=1  AND CustomerID LIKE '%[0-9]%'

When we execute above query all alphanumeric values in the field  will be ignored and the output will be 21.

Friday, February 24, 2012

Transaction in ASP.NET

I have seen many people asking questions in forum that how to use transaction in ASP.NET. Normally we do transaction from SQL. Here I am going to explain how to do the transaction from ASP.NET code behind.

Normally transaction will be used in the code where you do all your database operation.

Below is the sample code to show you how the code looks like when we use the transaction in ASP.NET code behind.

string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection SQLCon = new SqlConnection(strCon);
SQLCon.Open();
    SqlTransaction SQLTran = SQLCon.BeginTransaction("MyTrans");
    SqlCommand Cmd = new SqlCommand();
    Cmd.Transaction = SQLTran;
    Cmd.Connection = SQLCon;
    try
{
        Cmd.CommandText ="First SQL query to update/delete record";
        Cmd.ExecuteNonQuery();
        Cmd.CommandText ="Second SQL query to update/delete record";
        Cmd.ExecuteNonQuery();        
        SQLTran.Commit();
    }
    catch (Exception ex)
    {
    lblError.Text = "Error!! <br>+" + ex.Message.ToString();
        SQLTran.Rollback();
    }

In the code you would have noticed that roll back code is kept inside the catch block which means whole execution will be rolled back if there is an exception.

I hope now you are clear with the Transaction concept in ASP.NET

Thursday, February 23, 2012

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

" Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack" - This is one of the common error we see in forum where people request solution for it.

Below is a small explanation and resolution to resolve this issue,

This error normally occurs when we use Response.Redirect or Server.Transfer or Response.End in your code before completing the actual process the page was doing.

In this case what you have to do is

In place of Response.End you have to use HttpContext.Current.ApplicationInstance.CompleteRequest

and in place of Response.Redirect, use the Response.Redirect ("Paganame.aspx", false);

and in place of Server.Transfer, use the Server.Execute method

There is also a KB article related to this issue,
http://support.microsoft.com/kb/312629/EN-US/




Wednesday, February 1, 2012

Received winners of Star of the Year - 2011 Award from dotnetspider.com

Happy to inform you all the I have been selected as "Star of the Year for the year 2011" and given iPad2 as gift by www.dotnetspider.com.

Thank you all for the support and guidance especially to Tony John who is the founder and webmaster of dotnetspider.com

You may read the announcement here Star of the Year