How to connect .NET with mySQL?

mySQL is one of the most widely used open source databases around the world. Today this is the database engine driving up to 90% of PHP driven websites on the web. PHP has been the first love of mySQL and it will always have a special place. However, with other technologies coming to the fore like .NET mySQL has come up with its own data provider to help connect .NET applications with mySQL easier.

Most .NET developers would answer that they would use ODBC to connect with mySQL. For that they would most probably download the ODBC driver from the mySQL website, install the driver and then connect the .NET application through it thereby making their application have to communicate with the ODBC and then the ODBC communicate with the mySQL driver which will only slow down performance.

mySQL now provides the Connector/NET drivers for .NET applications which can be downloaded directly from their website. Download the MSI and run it. It will register the MySQL.Data assembly into the Global Assembly Cache. ASP.NET developers would most probably have to place the assembly into their Bin folder and use it.

I have used .NET Connector 5.2 and it worked fantastic with ASP.NET. No performance issues nothing. The best part is that the syntax is similar to the SqlClient namespace so there’s no learning curve and you can start development at once.

Download the .NET Connector 5.2 driver from here

Hope the above helps

What to do if you get “It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS”

Please view the following URL for cause and resolution of this problem
http://forums.asp.net/t/769696.aspx

Most of the times this occurs if you have a more than web.config defined in your sub folders.

How to create and use menu in ASP.NET?

To be frank I used to think that ASP.NET menu control was complex and hard to use. Boy was I wrong!!!

ASP.NET provides a menu control which allows you set all its visual properties and menu items and voila start using them. Not only that you can create a theme for it and with a bit of imagination you can have a snazzy looking menu ready for you in a couple of minutes.

Below is the code for a simple horizontal view menu. You can use it to enhance its look and feel.

<asp:Menu ID=”mnu” runat=”server”
DisappearAfter=”500″ MaximumDynamicDisplayLevels=”5″
Orientation=”Horizontal”>
<DynamicMenuItemStyle Font-Names=”Arial” Font-Size=”Small” BackColor=”#f7f7f7″ Width=”200″ />
<DynamicHoverStyle BackColor=”#ffff00″ />
<StaticMenuItemStyle BackColor=”#f7f7f7″ Font-Names=”Arial” Font-Size=”Small” />
<Items>
<asp:MenuItem Text=”About Us” NavigateUrl=”about_us.aspx”>
<asp:MenuItem Text=”Company” NavigateUrl=”company.aspx”></asp:MenuItem>
<asp:MenuItem Text=”Our Goal” NavigateUrl=”goals.aspx”></asp:MenuItem>
<asp:MenuItem Text=”Company” NavigateUrl=”company.aspx”></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text=”Products” NavigateUrl=”products.aspx”>
<asp:MenuItem Text=”Product Catalog” NavigateUrl=”catalog.aspx”></asp:MenuItem>
<asp:MenuItem Text=”Enquiry” NavigateUrl=”enquiry.aspx”></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>

Hope the above helps

How to add a value to asp:Checkbox control and retrieve it on postback?

The <asp:Checkbox> is one of those controls in ASP.NET which does not have a default Value property in which you can store a value and retrieve it upon postback. This becomes a problem in cases where the value has to be saved into the database or an action depends on the condition of that value.

There are several workarounds and depending on your coding acumen you can even develop your own but we will stick to the simplest here.

You can use the InputAttributes collection in the Checkbox control. You can add a value to the Checkbox as follows:

Suppose chkTest is the ID of the checkbox. You can set its value through the following code

chkTest.InputAttributes.Add(“value”,”Adeel”);

Refresh the page and view the source of the page. There you will find the value attribute in the chkTest checkbox code.

If you would like to retrieve the value of the checkbox on postback then use the following code.

Response.Write(chkTest.Attributes[“value”].toString());

Hope the above helps.

URL mapping in ASP.NET

I had a requirement from our SEO department for a website. They wanted to create some static URL sort of pages for a website they were working on and wanted those URLs to be placed on the website footer.

Now, the pages they required were similar to the pages that already existed on the website. For e.g. they wanted a page like /name1_name2.aspx where as it already existed in the format as /results_page.aspx?q1=name1&q2=name2.

They wanted a URL which did not have any strings (query string :)) attached to it.

Now I could have created separate pages and then used the same classes on the new pages as on the results page but I had a better idea. I knew ASP.NET provided some sort of url mapping feature through the web.config. So after some research I found it.

If you place the following code in the system.web section of your web.config you will achieve url mapping.

<urlMappings enabled="true">   
<add url="~/name1_name2.aspx" mappedUrl="~/results_page.aspx?q1=name1&q2=name2" />   
</urlMappings> 

If you run the some_page.aspx it will in actuality run the results_page.aspx without the visitor ever noticing. So, I just have to add as many entries as I want in the web.config file and voila it starts working.

Please note if you have a large number of files to map then please use URL Rewriting in ASP.NET than this.

Hope this helped you