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


Hi
I was facing error like
“Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack “.

when i had written code like this

Response.Redirect(url);

To solve this error, i made like this.

Response.Redirect(url,false);

How to make enable/disable particular row cell of Gridview ?



Hi
In our daily coding, so many time we have to do particular action in some particular cell. For example we have to do enable/disable some row cell of Gridview on some condition. Then we can do like this
Step1: Click on onrowdatabound event of Grid
Step2: Write the code in code behind like this

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

Button btn1 = (Button)e.Row.FindControl(“BtnSubmit”);

if (e.Row.RowType == DataControlRowType.DataRow)
{

Label LblEmpSal = (Label)e.Row.FindControl(“lblEmpSal”);
if (LblEmpSal.Text == “15000”)
{
btn1.Enabled = false;
}

}

}

How to call the webservice using Ajax on client side?


Hi
I hope that you are knowing the basic concept of web service.
There are so many method to call the web service.
1.Using Ajax (script manager control) and JavaScript
2.using Jquery.
3.Using C# code.

Here i m going to show you using script manager and Javascript.
Step1. Create the simple webservice like this, and UnComment this line
[System.Web.Script.Services.ScriptService]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

///
/// Summary description for WebService
///
[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

public WebService ()

{

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld() {
return “Hello World”;
}

}

Step2: Write the Javascript code in default page like this

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default2.aspx.cs” Inherits=”Default2″ %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
<script language=”javascript” type=”text/javascript”>
function onClick()

{

WebService.HelloWorld(OnComplete, OnTimeOut, onerror);

return true;
}
function OnComplete(args) {
document.getElementById(‘Label1’).innerHTML = args;
}
function OnTimeOut(args) {
alert(“Service call timed out.”);
}

function OnError(args)

{
alert(“Error calling service method.”);
}
</script>
</head>

<body>

<form id=”form1″ runat=”server”>
<div>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” >
<Services>
<asp:ServiceReference Path=”WebService.asmx” />
</Services>
</asp:ScriptManager>
<input id=”Button1″ type=”button”
value=”button” onclick=”return onClick();” />
<br />
<br />
<label id=”Label1″ style=”width: 318px”></label>

</div>
</form>
</body>
</html>

How to display jquery animated confirmation message in asp.net?


Hi
While working in one of my personal project, I used “displaying confirmation message using jquery”. It is very nice and easy to use.
Step1: Download the related jquery plugin and Write the HTML code like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title>Add New Customer</title>
<link href="Default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server" defaultbutton="Save" defaultfocus="Name">
<asp:ScriptManager runat="server">
<Scripts>
<asp:ScriptReference Path="~/jquery-1.2.6.min.js" />
<asp:ScriptReference Path="~/jquery.blockUI.js" />
<asp:ScriptReference Path="~/Default.js" />
</Scripts>
</asp:ScriptManager>

<div id="EntryForm">
<label for="Name">Name:</label>
<asp:TextBox runat="server" ID="Name" />

<label for="Address">Address:</label>
<asp:TextBox runat="server" ID="Address" />

<label for="City">City:</label>
<asp:TextBox runat="server" ID="City" />

<label for="State">State:</label>
<asp:TextBox runat="server" ID="State" />

<label for="Zip">Zip:</label>
<asp:TextBox runat="server" ID="Zip" />

<asp:Button runat="server" ID="Save" OnClick="Save_Click"
Text="Add Customer" UseSubmitBehavior="false" />
</div>

<asp:UpdatePanel runat="server" ID="upConfirmation">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Save" />
</Triggers>
<ContentTemplate>
<asp:Panel runat="server" ID="ConfirmSave" Visible="false">
<h3><asp:Literal runat="server" ID="CustomerName" /> added</h3>

<a href="#" id="CloseConfirm">Close</a>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

Step2:Write the code in code behind like this

using System;
using System.Web.UI;

public partial class _Default : System.Web.UI.Page
{
protected void Save_Click(object sender, EventArgs e)
{
// Save the customer to your data store here.
System.Threading.Thread.Sleep(2000);

// Display the confirmation.
ConfirmSave.Visible = true;

// You can custamize the confirmation.
CustomerName.Text = Name.Text;
}
}

Posted in Jquery. 1 Comment »

How to include caching in WebService ?


Hi
If our requirement is to fetch the same data again and again from database, then we can use caching concept in web service to increasing the performance and avoid the unnecessary hit to database.
Syntax for Caching in web service
[WebMethod(CacheDuration = 5)]
Where 5 is time duration in second.

Complete Syntax is like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

///
/// Summary description for TestCaching
///

[WebService(Namespace = “http://tempuri.org/&#8221;)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class TestCaching : System.Web.Services.WebService {

//This syntax is used for cache in webservice
[WebMethod(CacheDuration = 5)]
public string GetServiceTime()
{
return DateTime.Now.ToString();
}

}

How to disable cut,copy and paste of password in asp.net Textbox?


Hi

Here is the syntax for disable

<asp:TextBox ID="TextBox1" TextMode="password" oncopy="return false;" oncut="return false;" onpaste="return false;" runat="server"></asp:TextBox>

How to insert data in XML file using LINQ to XML?


Step1:Create one class i.e Emp.cs and declare the property like this

public class Emp
{
public int Id { get; set; }
public string EmpName { get; set; }
public string EmpSal { get; set; }
}

Step2: Create the Emp.xml file like this in Solution Explore

<?xml version="1.0" encoding="utf-8"?>
<Emps>

</Emps>

Step3: Write the code in code behind like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

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

}
protected void Button1_Click(object sender, EventArgs e)
{
XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("Emp.xml"));

XElement newEmp = new XElement("Emp",
new XElement("EmpName", TextBox1.Text),
new XElement("EmpSal", TextBox2.Text)
);
newEmp.SetAttributeValue("Id", GetNextAvailableID());

data.Element("Emps").Add(newEmp);
data.Save(HttpContext.Current.Server.MapPath("Emp.xml"));

TextBox1.Text = "";
TextBox2.Text = "";

FillGrid();

}

private static int GetNextAvailableID()
{
XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("Emp.xml"));

return Convert.ToInt32(
(from c in data.Descendants("Emp")
orderby Convert.ToInt32(c.Attribute("Id").Value) descending
select c.Attribute("Id").Value).FirstOrDefault()
) + 1;
}

protected void FillGrid()
{
var query = from a in XElement.Load(MapPath("Emp.xml")).Elements("Emp")
select new Emp
{

Id = Convert.ToInt32(a.Attribute("Id").Value),
EmpName = (string)a.Element("EmpName"),
EmpSal = (string)a.Element("EmpSal")

};

GridView1.DataSource = query;
GridView1.DataBind();
}
}

Posted in LINQ, XML. 2 Comments »