A very useful statement to check if a constraint exists before do something to it. Note, we can never edit a constraint before deleting it and re-create.
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA=’dbo’
AND CONSTRAINT_NAME=’FK_37_Page_FK’ AND TABLE_NAME=’PageContent’
For some reason, DataBinder.Eval(Container.DataItem, “id”) will not work in li tag within the item template of a repeater. I had to use this:
((System.Data.DataRow)Container.DataItem)["id"].ToString()
UserControls are great, particularly in making the code more eye friendly, seperating big bunch of codes into different parts and be able to re-use them throughout the application. However, there are times when we need to reference something in the parent page or control from the usercontrol level. That’s when I had an issue today, and finally solved it using the brilliant delegate.
Here’s how it work:
Suppose I have a parent page that has these:
1. UserControl
2. Label
The UserControl itself is a web form with a number of fields. Now, what I want to do is whenever someone fills in the form (usercontrol), the result will get sent back to the parent and displayed in the label. With the use of delegate, this can be done easily:
In the usercontrol, simply create a delegate:
public delegate void FormSubmittedHandler(object sender, EventArgs e);
This has to be outside the class.
Read the rest of this entry »
Having a working sitemap in an ASP.NET application can’t be anymore important. You can use it for navigations, breadcrumbs, etc. The default web.sitemap file is an XML file which provides the root node of the site, the SiteMapProvider used, and tracks the provider objects. System.Web.SiteMap inherits from System.Object. The default provider XMLSiteMapProvider works through the Web.sitemap. Besides being useful for site navigation, the sitemap also provides a easy way for search engines to look for pages on the site.
However, hard coding these urls in the sitemap file isn’t a solution for dynamic website, for example one that is managed by a content management system. Pages are constantly generated. So, the sitemap would have to be updated everytime we create a new page or a new url is created. To do that, we could use a dynamic sitemap provider:
Read the rest of this entry »
Have been playing a lot with a number of WYSIWYG text editors, such as RadEditor and many other free ones, but the one that I liked the most would be FCKEditor, which is open source editor, written in Javascripts and support a wide number of languages like PHP, ASP, JAVA, .NET, CFM, etc. It’s extremely easy to integrate, especially in .NET.
Steps to install and integrate:
1. Download and extract the whole folder into your web application root.
2. Download the .NET component and add into the reference.
3. Boom, use it:
FCKeditor txbFckEditor = new FCKeditor();
txbFckEditor.ID = “txb” + strColumn;
txbFckEditor.Height = 400;
txbFckEditor.Width = 700;
txbFckEditor.BasePath = Page.ResolveUrl(”~/Fckeditor/”);
A very handy function to grab any query string using Javascript:
function getQueryString( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
I have been playing around with a free datagrid control available, called FlexiGrid. It has to be one of the very best grid control available as open source. Having a very elegant user interface and very easy to integrate and customised codes, we could integrate FlexiGrid into any existing applications. The beauty of FlexiGrid is simply using the strength of JQuery that supports functionalities such as resizable column widths, show/ hide columns, pagination, etc. It also supports JSON/ XML with web services. Meaning, everytime we browse through pages, it actually communicates with a web service, which returns back an XML that gets parsed and displayed in the grid.

Read the rest of this entry »
A very useful command to convert dataset or datatable to an XML Document:
// This is the final document
XmlDocument Data = new XmlDocument();
// Create a string writer that will write the Xml to a string
StringWriter stringWriter = new StringWriter();
// The Xml Text writer acts as a bridge between the xml stream and the text stream
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
// Now take the Dataset and extract the Xml from it, it will write to the string writer
content.WriteXml(xmlTextWriter, XmlWriteMode.IgnoreSchema);
// Write the Xml out to a string
string contentAsXmlString = stringWriter.ToString();
// load the string of Xml into the document
Data.LoadXml(contentAsXmlString);
Enterprise Library is a very useful component developed by Microsoft to help developers solving many common tasks. One that I use a lot is the data access block, which allows you to run data access query in just one line of code! (Saves all the work in writing a command and pass to a reader, etc).
To use Enterprise Library, all have to do is:
1. Download the component and install.
2. Add reference to your project (Microsoft.Practices.EnterpriseLibrary.Data and Microsoft.Practices.EnterpriseLibrary.Common).
3. Configure your web.config:
//under configsections
Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data
Set a default database:
dataConfiguration defaultDatabase="AdventureWorksDB"
Now we can query data access in just one line of code:
DataTable dtt = DatabaseFactory.CreateDatabase().ExecuteDataSet(”StoredProcName”).Tables[0];
Some basic scripts to add/ modify primary key and foreign key constraints in SQL:
--create a primary key constraint
ALTER TABLE DO_DataClass
ADD CONSTRAINT pk_id PRIMARY KEY (id)
--create a foreign key constraint which will perform the cascade delete action
Alter table DO_DataClass add constraint DO_DataClassGroup_FK
Foreign Key (FK_DO_DataClassGroup) references DO_DataClassGroup (id)
on delete cascade
go