Infinite Menus, Copyright 2006, OpenCube Inc. All Rights Reserved.  
 
 
Support Centre - Databases


- ASP.NET application determine whether cookies are enabled
- ASP.net application to display non-English language
- Connect ASP.net application to MySQL
- Invalid Viewstate on ASP.NET pages
- Sending emails in ASP.NET 2.0
- Sending mail from ASP.NET
 
ASP.NET application determine whether cookies are enabled


Determining whether cookies are enabled requires a round trip to the browser. The basic strategy is to return a cookie in an HTTP response and redirect to a page that checks for the cookie.

Here's a the scenario:

<html>
<body>
<form runat="server">
<asp:Button Text="Cookie Support" OnClick="OnTest" RunAt="server">
</form>
</body>
</html>

<script language="C#" runat="server">
void OnTest (Object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie ("UserIdentifier", "Bob");
Response.Cookies.Add (cookie);
Response.Redirect ("OtherPage.aspx");
}
</script>

And here's the page that it redirects to (OtherPage.aspx). This page uses the presence or absence of the cookie to determine whether cookies are enabled and displays the result:

<%@ Page Language="C#" %>

<html>
<body>
<%
HttpCookie cookie = Request.Cookies["UserIdentifier"];
if (cookie != null && cookie.Value == "Bob")
Response.Write ("Cookies are enabled");
else
Response.Write ("Cookies are not enabled");
%>
</body>
</html>

 
ASP.net application to display non-English language


ASP.net allows the developer to encode the page using character sets other than unicode. If your ASP.net page containing foreign characters does not render properly when served from our webservers, you may need to change the FileEncoding setting in the web.config file.

<configuration>
<system.web>
<globalization
fileEncoding="<code page>"
requestEncoding="<code page>"
responseEncoding="<code page>"
culture="en-US"
uiCulture="de-DE"
/>
</system.web>
</configuration>

For more information on code page, please review these articles:
*
http://www.microsoft.com/globaldev/getWR/steps/wrg_codepage.mspx
* http://www.microsoft.com/typography/unicode/cscp.htm
 
Connect ASP.net application to MySQL


Methods can be used to connect an ASP.net application to MySQL database.

1. Using a ODBC .net data provider
2. Using a MySQL Data Provider

1. ODBC version

using System.Data.Odbc;

......

string connString = "DRIVER={MySQL ODBC 3.51 Driver};server=mysql101.isqsolutions.com; user=dbUsername;password=dbPassword;database=dbName;";

OdbcConnection odbcConn = new OdbcConnection(connString);
odbcConn.Open();

2. MySql Data Provider

You will need to import the MySql.Data assembly into your project.
You can get it here:

http://dev.mysql.com/downloads/connector/net/1.0.html

It is used like this:
using MySql.Data.MySqlClient;

.....

string connString = "Data Source=mysql101.isqsolutions.com;Database=dnName;User
Id=dbUsername;Password=dbPassword;";

MySqlConnection objConnection = new MySqlConnection(connString);
objConnection.Open();

 
Invalid Viewstate on ASP.NET pages


You may receive the following error message from ASP.NET pages.

[HttpException (0x80004005): Authentication of viewstate failed. 1) If this is a cluster, edit configuration so all servers use the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster. 2) Viewstate can only be posted back to the same page. 3) The viewstate for this page might be corrupted.]

Viewstate represents the state of the page when it was last processed on the server. The contents of the page are stored in a container and moved to and from the server on each request. By default, ASP.NET validates the contents of viewstate to ensure that it has not been tampered. If this validation test fails, an invalid viewstate exception is thrown. One of the issues that cause this test to fail is the recycling of the AppPool where the website lives.

IIS 6 will periodically recycle the application pool to maintain the health of the application pool. At the instance when the application pool is being recycled, browser requests may sometimes result in an invalid viewstate error.

The workaround is to use a specific key in the Web.config to prevent automatic key generation on each process start. The key must be exactly 128 bits made up of random characters and inserted into the configuration file on each webserver experiencing the problem. An example follows.
<<machineKey validationKey=
"EE94EFA0280BBF3A1FE0E8DC5FE9EB725C3CE9D0E59187EC91CF32B463241B4E D6A60E3152B158D30B5BD45D99F09D504D3F75AE860605A11806EDBEF88DAB3F"
decryptionKey=
"24996804B8C6C416BF7AE6BE7337AE590E404C00A91DB274" validation="SHA1">

You can generate a pair of keys at the following page:

http://www.aspnetresources.com/tools/keycreator.aspx
 
Sending emails in ASP.NET 2.0


// using System.Net.Mail;
// using System.Net;

SmtpClient smtpClient = new SmtpClient("smtp.domainHosted.com");

smtpClient.Credentials = new NetworkCredential("user1@domainHosted.com", "passwordHere");

MailMessage message = new MailMessage();

message.From = new MailAddress(user1@domainHosted.com);
message.To.Add(new MailAddress(nick@example.com));
message.To.Add(new MailAddress(ben@example.com));

message.Subject = "This is my subject";
message.Body = "This is the content";

smtpClient.Send(message);

 
Sending mail from ASP.NET


// using System.Web.Mail - this example is for ASP.NET 1.1;

MailMessage eMail = new MailMessage();

eMail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
eMail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "account@domainHosted.com";
eMail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "password";

eMail.To = "someone@yahoo.com"; // Recipients
eMail.From = "somebody@yahoo.com";
eMail.Subject = "This is the subject line";
eMail.Body = "Test Message";

SmtpMail.SmtpServer = "smtp.domainHosted.com";
SmtpMail.Send(eMail);

To test, just change domainhosted.com with the domain name and use one existent email account to authenticate in the smtp server.
To create an email account, log in the hosting manager.

These examples may not work for you without certain modifications.
The purpose of the examples is to give you a general idea on the topic.

 
 
  Support Centre - FAQ
ASP.net
Databases
Coding Components
Scripting Support
Downloads

Demo LiveStats
Demo WebMail
Demo Hosting Manager
 
Disclaimer Privacy Policy Service Guarantee Terms & Conditions

807-966 Inverhouse Dr., Mississauga-ON, L5J4B6 - contact@tj-hosting.com
Phone: 905-919-9949 - Fax: 905-823-5895 - Toll-Free: 1-866-818-3162
© 2004-2014 TJ-Hosting - All Rights Reserved.