C# Tutorials

How to connect to MySQL database using ASP.NET hosting

Many developers use ASP.NET with MySQL with their applications. If you use ASP.NET hosting services and some of the .NET stack as VB.NET or C# then you could connect to the MySQL database using ODBC .net data provider.

Connect to MysQL database using C# using ASP.NET hosting

OdbcConnection cn = new OdbcConnection(“Driver={MySQL ODBC ODBC 5.2a Driver};Server=localhost or remote;Database=test;User=user;Password=pass;Option=3;”);

OdbcCommand cmd …

Learn more

How to disable a button after click in ASP.NET

This is a very common scenario when you would like to disable button after click, so the user could not click it again if you need some time to do some task which takes longer time to be executed.

Disable a button after click in ASP.NET before the PostBack

If you would like to disable button to do some task, …

Learn more

How to apply CSS style in the Label control from code-behind in ASP.NET in C#

You can apply CSS style from code behind by accessing the properties of the control with the help of its Id. Most of the CSS styles can be applied using the Font property and its sub properties of the Label control. However you can use ForeColor and BackColor directly from the Label control.

TestApplyCSS.aspx

<%@ Page Language=”C#” AutoEventWireup=”false” …

Learn more

How to change the text of the Label control from the code-behind in C#

TestPage (.aspx)

<%@ Page Language=”C#” AutoEventWireup=”false” CodeBehind=”TestPage.aspx.cs” Inherits=”TestChange.TestPage” %>

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

<html xmlns=”https://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title>Change the text of the Label control from the code-behind</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:Label ID=”LabelTest” runat=”server” Text=”My test label”></asp:Label>

</div>

</form>

</body>

</html>

Test page code-behind (.cs)

//

// TestPage.aspx.cs

//

protected void Page_Load(object sender, EventArgs e)

{

// changing text from …

Learn more

How to transfer information between pages in ASP.NET using a query string in C#

Software developer can use this approach for information that don’t need to be hidden or made tamper-prof. Unlike view state information passed through the query string is clearly visible and unencrypted. The query string is the portion of the URL after the question mark in the following example:

https://www.google.com?q=look+for+aspnet+code

Software developer can store information in the query string himself using …

Learn more

How to store custom objects in a View state using C#

Software developer can store custom objects in a view state just as easily as regular types. However, to store an item in a view state, ASP.NET must be able to convert it into a stream of bytes. This process is called serialization. If developer’s objects aren’t serializable, he will receive an error message when he attempts to place …

Learn more

How to set focus on control in C#

You can put the focus on the following types of ASP.NET controls: Button, LinkButton, and ImageButton controls, CheckBox control, DropDownList control, FileUpload control, HyperLink control, ListBox control, RadioButton control, TextBox control.

The following C# code example shows how to set the focus on the control with the ID TextBox1:

protected void Page_Load(object sender, EventArgs …

Learn more

How to preserve member variables for an ASP.NET page in C#

Software developers can follow the next basic principle. They can save all member variables to View state when the Page.PreRender event occurs and retrieve them when the Page.Load event occurs. The Page.Load event happens every time the page is created. In case of a postback, the Load event occurs first, followed by any other control events. The next …

Learn more