Thursday, December 31, 2009

Microsoft Ajax Minifier 1.1

There is a new release of microsoft ajax minifier on codeplex.com

What Ajax minifier does?

Ajax Minifier reduce the size of your JavaScript files to improve the performance of you Ajax enabled site.

What does it support?

The Microsoft Ajax Minifier supports two levels of minification: normal crunching and hypercrunching. Normal crunching refers to the process of removing unnecessary whitespace, comments, semicolons, and curly braces. Hypercrunching refers to the process of shortening the names of local variables and removing unreachable code.

 

You can review the project at

http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=35893

Wednesday, December 30, 2009

Dynamic Populate Extender example Ajax Control Toolkit, manual populate control

Ajax control toolkit have control "Dynamic Populate Extender".

Dynamic populate extender is a control which will replace html/content of any control based on the web service result.
To populate you need to create a web service method which have single parameter with name "contextKey". If you do not specify contextKey variable or you make its name different then your extender will fire error "Web Service call failed: 500".

There are 2 ways to populate content.

Dynamic Populate option 1) You can specify attribute PopulateTriggerControlID="YourControlId" and then you click on the control and your data will be populated

Dynamic Populate option 2) You can write a javascript code which will call the populate event of Dynamic Populate Extender.
Script is

   1:  <script type="text/javascript">
   2:  function UpdateControl(value) {
   3:  var behavior = $find('dp1');
   4:  if (behavior)   {
   5:       behavior.populate(value);   
   6:      }
   7:  }
   8:  </script>

Based on above script “value” will be passed as “contextKey” in the web service.

Html code to for this example is

   1:  Enter value
   2:  <input type="text" id="txtConetnt" onblur="UpdateControl(this.value);" />
   3:  <asp:Panel ID="Panel1" runat="server"></asp:Panel>
   4:  <ajaxToolkit:DynamicPopulateExtender ID="DynamicPopulateExtender1" runat="server" 
   5:      TargetControlID="Panel1" BehaviorID="dp1" ServiceMethod="GetValue">
   6:  </ajaxToolkit:DynamicPopulateExtender>

Web service code for this example is

   1:  'VB
   2:  <System.Web.Services.WebMethod()> _
   3:  <System.Web.Script.Services.ScriptMethod()> _
   4:  Public Shared Function GetValue(ByVal contextKey As String) As String
   5:    Return String.Format("<span style='font-family:courier new;font-weight:bold;'>{0}</span>", "You entered value " + contextKey)
   6:  End Function
   1:  //C#
   2:  [System.Web.Services.WebMethod]
   3:  [System.Web.Script.Services.ScriptMethod]
   4:  Public string GetValue(string contextKey)
   5:  {
   6:       return String.Format("<span style='font-family:courier new;font-weight:bold;'>{0}</span>", "You entered value " + contextKey);
   7:  }

 

Hope this example help you to use Dynamic Populate Extender.

Error during save changes of sql table design in SQL server management studio 2008.

In SQL Server Management Studio 2008,if we open table in design mode to make changes and then click save after making changes in table structure it will give error.

Error says.



this is due to there is "Prevent Saving changes that require table re-creation" option is enabled in SSMS-2008.



To work around this Just Un-Check "Prevent Saving changes that require table re-creation" option from Tools > Options >Designers.

hope this will help you.

Thanks.


Tuesday, December 29, 2009

Various URL rewriting methods in ASP.Net C# Overview


When we have multiple parameters in our URL, at the time it is possible that hackers can identify our logic based on parameter and try to get into the system with those parameters. To resolve this issue we can use URL rewriting. URL rewriting is the process of hiding our parameterized URL and make it simple, browser and crawler friendly URL.
Example:
URL rewriting is also useful for Search Engine Optimization (SEO). When you use parameter on your single page and load different data. Search engine (i.e. Google) will consider it as single page. But when you use URL rewriting it will consider that all pages are different.

Ways of Programming

In .Net there are 3 ways to achieve URL Rewriting.
1.       With Http handler factory
2.       With Http Module
3.       Global.asax

URL Rewriting with Http Handler Factory

Benefits

When you use handler based URL rewriting you can specify that for particular extension this handler will be called. So for each request your code will not execute. What handler factory does is select appropriate handler based on your code and returns it to compiler to handle the request.

Disadvantages

When you want to handle directory without any page you need to specify wild card for the handler factory. In this case each request will be go through your code. And for image or static files it will fire error (if not handled) that cannot parse file. To avoid this issue you can specify static file handler in web.config file above the wild card (such as .jpg, .gif), so your file will be treated as static.

URL Rewriting with Http Module

Benefits

When you are using this method you do not require to specify any extension or wild card in web.config. All file will be checked for rewrite and if any code is not available then it will load file as normal http request.

Disadvantages

In this case request for each either a page or static file like images will pass through the module. So it will cause more processing. If the code is not proper then it will execute each of your rule/code for all requests.

Global.asax

Benefits

This will work same as http module.

Disadvantages

This method does have one more disadvantage over http module. In IIS 7 some of methods are restricted so you either need to recode or need to go over http module.

Friday, December 18, 2009

Insert/Update/Delete data in sql-xml column

How to manipulate data in Untyped(sql-xml) column.

With advent of xml datatype in sql server, developer get a power of xml in sql server. with the help of "XML DML" we can insert,update node in sql-xml column.
this sql-xml column also known as Untyped column.

we can reduce number of records in table by using this xml column wisely.
for example let's say we have a "country" table which stores name and ID of country in SQL table.now if we want to add state for all country, we have a separate table "state" and for each state there will be separate entry in table along with same country id.

we can handle this situation by storing all state of that country in "country" table as xml nodes.

At many points you will find this sql-xml data type helpful and powerful.this datatype can be queried with almost same concept as xml query except some syntax variation.

declare @Country as table(ID int IDENTITY(1,1) PRIMARY KEY,Name varchar(20),States xml)
INSERT INTO @Country
select 'India','<states><state ID="1">Gujarat</state><state ID="2">Rajasthan</state><state ID="3">AP</state><state ID="4">MP</state><state ID="5">UP</state></states>'
union all
select 'USA','<states><state ID="1">Alaska</state><state ID="2">Alabama</state><state ID="3">Florida</state><state ID="4">Idaho</state></states>'
union all
select 'Uk','<states><state ID="1">Uk1</state><state ID="2">Uk-2</state></states>'


/*SELECT DATA:: this will select all state from table where ID=1(India)*/

declare @States xml
set @States=(select states from @Country where id=1)
select tab.col.value('@ID','int') as ID,tab.col.value('.','varchar(20)') as Name from @States.nodes('states/state') tab(col)
--where tab.col.value('@ID','int')=1
print 'State Selected'

/*INSERT DATA::this will insert a state where id=1(India) with Name "Arunachal Pradesh"*/

Update @Country
set States.modify('insert<state ID="6">Arunachal Pradesh</state>into (/states)[1]')
where ID=1
print 'State Inserted'

/*UPDATE DATA::this will update name of state with id=6 to Arunachal and country id=1*/

Update @Country
set States.modify('replace value of(/states/state[@ID=6]/text())[1] with "Aurnachal"')
where ID=1
print 'State Updated'

/*DELETE DATA::this will delete where Id=5 (UP) and countryid=1 from sql-xml column*/

Update @Country
set States.modify('delete (/states/state[@ID=5])')
where ID=1

print 'state Deleted'

/*
http://www.simple-talk.com/sql/t-sql-programming/xml-data-modification-language-workbench/
*/

hope this will help you.

thanks.

Monday, December 14, 2009

WrapText function in javascript.

Some times in highly user interactive sites like forums and community domain. User may enter continues text as long word and which will not be warped automatically.
To wrap text it needs White spaces between words, if it can't find space it will distort our design and looks of page will not remain as it is. to handle such condition we can split long word after specified characters.
we can do this server side or client side. i have written a function in Javascript.Called WrapText.
Regular expression is the best way to do this task with effective and minimal code.
you can use this as per you need ,or using same Regular expression you can write function in C# or VB.
here is the function in javascript.
<script type="text/javascript" language="javascript">
function WrapText(Input) {
var FillChar = "</br>";
var MaxLength = 10;
var pattern = new RegExp("([^\\s-]
{" + MaxLength + "})", "g");
var myString = Input;
return myString.replace(pattern, "$1"+FillChar);
}
alert(WrapText('HI_JAVSCRIPT_HOW_YOU_DOING'));
</script>

Thanks,

How to Insert/Update in XML File.

There isn't any direct update and insert methods available for XML document . we have logically to append and remove child nodes from xml document.
There is good stuff provided for managing xml document in "System.Xml" name space in .net.
here are the steps to perform this simple but most handy task whilse development.
(1) string xmlPath = Server.MapPath("~/states.xml"); //Get path of xml file
(2) XmlDocument xDoc = new XmlDocument(); //Use XmlDocument class to perform operation.
(3) xDoc.Load(xmlPath); //Load xml file in to xml document.

(4)
string xNodeFilter = "/states/state[@ID='" + StateID + "']"; //Create filter for Select Node if it it already Exists in File.

(5)
XmlNode xNodeState = xDoc.SelectSingleNode(xNodeFilter); //Select Node if it it already Exists in File.

(6)
if (xNodeState != null) { xDoc.ChildNodes[0].RemoveChild(xNodeState); } //Remove Node if it is already there in File

(7)
XmlAttribute xAttrID = xDoc.CreateAttribute("ID"); XmlAttribute xAttrRegionID = xDoc.CreateAttribute("RegionId"); XmlAttribute xAttrName = xDoc.CreateAttribute("Name"); XmlAttribute xAttrColor = xDoc.CreateAttribute("Colour");

//Create Attribute of node to be added.

(8)
xNodeState = xDoc.CreateNode(XmlNodeType.Element, "state", "");//Create node to be added.

(9)
xAttrID.Value = "1"; xAttrRegionID.Value = "0"; xAttrName.Value = "Gujarat"; xAttrColor.Value = "#00ff00";//Set Values Of attribute just created.

(10)
xNodeState.Attributes.Append(xAttrID); xNodeState.Attributes.Append(xAttrRegionID); xNodeState.Attributes.Append(xAttrName); xNodeState.Attributes.Append(xAttrColor); //Append Attribures to node.

(11) xNodeState.InnerXml="This is my mother land"; //Set Inner data of Node

(12) xDoc.ChildNodes[0].AppendChild(xNodeState); xDoc.Save(xmlPath); //Add child to document and Save document.
This are the steps. after putting this steps.please make sure that xml file is not read only.
hope this will help you.
Thanks.

Read XML In to the SQL table.

We can read whole xml file into the sql table using Bulk Copy command in SQL server. some time this command gives Exception "you do not have permission to use the bulk load statement". It says that you must have assigned rights to execute bulk load statement.this can be assigned by database administrator.

After reading file from specific location using bulk load command we can query it using built-in SQL commands, which are shipped with installation sql server 2005.

As sql server 2005 supports XML datatype as native datatype. it will be very easy to convert xml into SQL table.

Here is the code to read from xml file and out them as sql table.

declare @xml as xml

declare @xml xml SELECT @xml = BulkColumn FROM OPENROWSET(BULK 'c:\states.xml', SINGLE_BLOB) TempXML; SELECT Tab.Col.value('@ID','varchar(2)') as ID,Tab.Col.value('@Name','varchar(20)') from @xml.nodes('states/state') Tab(Col)

hope this will help. Thanks.