Monday, April 21, 2008

Disable Copy and Paste for greater website security

<body bgcolor="#FFFFFF" ondragstart="return false" onselectstart="return false">

Removing Xml element in two ways

<?xml version="1.0" encoding="utf-8" ?><bookstore> <books> <book genre="reference"> <title>World Atlas</title> </book> <book genre="reference"> <title>World Atlas</title> </book> </books></bookstore>


XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("test.xml"));
XmlElement root = xmldoc.DocumentElement;


root.RemoveChild(root.SelectNodes("books/book"));
root.RemoveChild(root.RemoveChild(root.FirstChild));

Replacing xml attribute in two ways

<?xml version="1.0" encoding="utf-8" ?><bookstore> <books> <book genre="reference"> <title>World Atlas</title> </book> <book genre="reference"> <title>World Atlas</title> </book> </books></bookstore>

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("test.xml"));
XmlElement root = xmldoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("books/book");
////////////////////////////////Replaceing Attribute value using xml///////////////////////////////
foreach (XmlNode xn in nodes)
{
xn.Attributes[0].Value = "NA";
}
////////////////////////////////Replaceing Attribute value using xml///////////////////////////////
foreach (XmlNode xn1 in nodes)
{
XmlNode dd = xn1.SelectSingleNode("@genre");
}

How to get the path for "My Documents" and other system folders?

Response.write( Environment.GetFolderPath( Environment.SpecialFolder.Personal ) );

Sunday, April 20, 2008

JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON is built on two structures:
1) A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
2) An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
Installation for Dotnet
http://sourceforge.net/project/showfiles.php?group_id=205597

An easy way to build connection string.

Though this in not related to .NET directly but it is useful while working with ADO.NET
1) Open a New notepad and save it with "udl" extension, suppose "New.udl".
2) Now you will see that it's icon is changed.
3) Open it, you will find Data Link properties dialog box.
4) For SQl Server connection string select Microsoft OLE DB Provider For SQL Server in Provider Tab.
5) Click button "Next" or select Connection Tab
6) Here you can select all connection details and press button
Test Connection. If it is successful close this dialog box.
7) Now open this file using "Notepad", you will find the connection string. Though it is built for OLE DB
type of connection, you can use for SQL Server connection by removing Provider attribute.
NOTE: If you are using SQL Authentication with password, then check the checkbox Allow Saving Password.
This is necessary so that password appears in connection string.

Thursday, April 17, 2008

Paging In Sqlserver 2005

SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY person) AS
rownum, person, income FROM Salaries) AS Salaries1
WHERE rownum >= 5 AND rownum <= 9
ORDER BY income

Wednesday, April 16, 2008

How to show Modal and Modeless dialog windows in Javascript

How to show Modal and Modeless dialog windows in Javascript

When you show a modal dialog the window remains on top of other windows until the user explicitly closes it.


window.showModalDialog("Test.html","dialogWidth:400px; dialogHeight:225px; status:no; center:yes");

When you show a modeless dialog the window remains on top of other windows, but you can still access the other windows.


window.showModalessDialog("Test.html","dialogWidth:400px; dialogHeight:225px; status:no; center:yes");

How do we get ipaddress or hostaddress using c#

How do we get ipaddress or hostaddress using c#

HttpContext.Current.Server.MachineName

HttpContext.Current.Request.ServerVariables["local_addr"]

Changing Connection string dynamically using Application_BeginRequest

You developed webapplication, you hosted in two different server.One server name is
"Production" and another server name is "Testing". The below code connection string will change dynamically based on server host information.

Example:
<appsettings>
<add key="production" value="http://www.production.com">
<add key="ProductionconnectionString" value="server=localhost;...." >
<add key="testconnectionString" value="server=localhost;...." >
</appsettings>

Public Application_BeginRequest()
{
bool production=false;
if(Request.URL.Host==ConfiqurationManager.Appsettings("production"))
{
Production=true;
}
}

if(Production==true)
{
context.items.add("connectionStrings",ConfiqurationManager.Appsettings("production"))
}
else
{
context.items.add("connectionStrings",ConfiqurationManager.Appsettings("test"))
}
}

Support Multiple Browser Image control

You are developing a web application that must support a variety of browser. Your analysis indicates that the majority of users that will access Internet explorer 6.0 and packet internet explorer 4.0 you are creating the header for the webapplication. you want to display variety of brower. The below example you can see image display with different browser.

Example:


<asp:image id="cklogo" runat="server" ImageUrl="Images/ck_logo.jpg" PIE4.0:imageurl="Images/small/ck_logo.jpg">

Thursday, April 10, 2008

Cross Joins Using sqlserver

Using Cross Joins
A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. This is an example of a Transact-SQL cross join:

USE pubs
SELECT au_fname, au_lname, pub_name
FROM authors CROSS JOIN publishers
ORDER BY au_lname DESC

The result set contains 184 rows (authors has 23 rows and publishers has 8; 23 multiplied by 8 equals 184).

However, if a WHERE clause is added, the cross join behaves as an inner join. For example, these Transact-SQL queries produce the same result set:

USE pubs
SELECT au_fname, au_lname, pub_name
FROM authors CROSS JOIN publishers
WHERE authors.city = publishers.city
ORDER BY au_lname DESC

-- Or
USE pubs
SELECT au_fname, au_lname, pub_name
FROM authors INNER JOIN publishers
ON authors.city = publishers.city
ORDER BY au_lname DESC

Accessing server event using Ajax

Step:1

Install system.web.extension and ajax tools
Install ajax.dll in your system all add in reference of the project.

step:2
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>

step:3
Write javascript

<script language="javascript" type="text/javascript">
function Getconfirm()
{
var con=confirm("Are sure want to delete this");
var test;
if(con==false)
{
alert(Bala.Getvalue().value);
}
else
{
alert(Bala.Getvalue1().value);
}
}
</script>

step 4:
in c# page


public partial class Bala : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Bala));
//btnsubmit.Attributes.Add("onclick", "Getconfirm();");
}
[Ajax.AjaxMethod()]
public string Getvalue()
{
return "bala";

}
[Ajax.AjaxMethod()]
public string Getvalue1()
{
return "bala1";

}
}

Monday, April 7, 2008

Importan queries using Sqlserver

http://www.dotnetspider.com/kb/Article4097.aspx

Check if a date is a valid date in Sql Server 2005

Use the ISDATE() function

The ISDATE() function determines whether the variable or the expression contains a valid date. It returns 1(true) if the input expression is a valid date;

otherwise, it returns 0 (false).

For eg:

DECLARE @dt varchar(10)

SET @dt = '02/21/08'

SELECT ISDATE(@dt)-- Returns 1

DECLARE @dt varchar(10)

SET @dt = '13/21/08'

SELECT ISDATE(@dt)-- Returns 0 as 13 is not a valid month

Reset all control values in .NET Web pages.

private void ResetFields()
{

foreach (Control ctrl in this.Controls)

{if (ctrl is TextBox)

{

TextBox tb = (TextBox)ctrl;

if (tb != null)

{ tb.Text = string.Empty;

}

} else

if (ctrl is DropDownList)

{ DropDownList dd = (DropDownList)ctrl;

if (dd != null)

{

dd.SelectedIndex = 0;

}

}

}

How to Show Binary Files in the Browser using ASP.NET and VB.NET

This article explains how to show binary files as well as .doc .xls files in browser. We have seen in many sites that user can open .gif, .jpg, .doc and .xls files by clicking a link. The binary format file as well as .doc .xls is displayed in the browser
1)Create an asp.net web project. Name the project as ShowBinary
2) Add a file of type (.gif,.jpg,.doc,.xls) to the project
3) Specify a Start page for the application4) Use following code snippet in the form load event handler

Private Sub Page_Load(sender as object, e as System.EventArgs)
Response.ContentType = “Application/pdf” ‘ Specify Content Type
Dim fpath as string = Mappath(“Mypdf.pdf”) ‘ Specify Physical Path of the file
Response.writefile(fpath)
End Sub

ASP.NET 2.0 MultiView Control

ASP.NET 2.0 Provies a wonderful solution by means of MultiView Control. The MultiView Control combined with the View Control can provide different UI on different scenarios as and when required.

<asp:MultiView ID="MultiView1" runat="server" >
<asp:View ID="View1" runat="server">This is one section where you can have a set of controls / contents<asp:View>
<asp:View ID="View2" runat="server">This is another section where you can have a different set of controls / contents<asp:View>
</asp:MultiView>

Now, to show the different tabs based on different events, we just need to set the following code:-

protected void Button1_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View1);
}
protected void Button2_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View2);
}

Multiview control using

"goto" Using Dotnet framework 2.0

for (int k = 0; k < 2; k++)
{
Response.Write("Testing Goto");
goto Testing;
Testing:
Response.Write("Testing Goto in for loop");
}

output
Testing GotoTesting Goto in for loop