Hi
If you dont like the page loading while page is postback and the page refereshes.
Then try it really very nice feature and make your page loading amazing, really i do that with one of my website. And it loads with fabulous effects. No need to write nasty of codes just put that tag on your master page. and see your site :)
< meta http-equiv="Page-Exit" content="progid:DXImageTransform.Microsoft.Fade(duration=.5)"/>
Keep Smiling :)
Thursday, April 16, 2009
Wednesday, April 15, 2009
Add Favicon to your site (Icon that dispays at the start of the url)
Want to add a beautiful icon at the start point of your URL (Called Favicon). Just add this tag to your master Page in the < head> section and before the < /head> Section :
< link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
and place an icon named favicon.ico on the Root directory.
When you add this site to add to favorites it will display the icon along with url. For doing that just add the following code also:
< a href="javascript:window.external.AddFavorite('http://www.test.com', 'Test Site')">
Click here to add Test Site to your favorites < /a>
it's a cool Feature..
< link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
and place an icon named favicon.ico on the Root directory.
When you add this site to add to favorites it will display the icon along with url. For doing that just add the following code also:
< a href="javascript:window.external.AddFavorite('http://www.test.com', 'Test Site')">
Click here to add Test Site to your favorites < /a>
it's a cool Feature..
Error Handling in Asp.Net
Asp.Net provide there way of handling errors. We can handle error at
1) Page Level
2) Application Level
3) Through Web.Confing
1) Page Level Error Handling :
We can handle error at page level so that if any error comes under the page scope it will handle through the OnError Method of page, But its only at page level not outside that scope. Whenever we create a web form its class drive from System.Web.UI.Page and that why page object help us to invoke page level error. What we do we override OnError method of the page. Like
protected override void OnError(EventArgs e)
{
Exception objException = Server.GetLastError (); // Return Current Exception
string errorDescription =
"Source of Error:" + objException.Source +
"Message : " + objException.Message +
"Stack trace: " + objException.StackTrace;
Response.Write (errorDescription); // Or you can log the Error or redirect to somewhere else/
}
2) Application Level Error Handling :
Another approach for error handling is at application level. Whenever any Error comes Application_Error sub called. Its a good place for handling error because no need to write it on every page. It Works something like that:
void Application_Error(object sender, EventArgs e)
{
Exception objException = Server.GetLastError (); // Return Current Exception
string errorDescription =
"Source of Error: " + objException.Source +
"Message : " + objException.Message +
"Stack trace: " + objException.StackTrace;
// After that use that Error errorDescription as you want eithr make log or redirect to somewhere else/
}
3) Web.Config Custom Errors
Third place is Web.Config, it enables us to configure html error pages to be displayed in place of a error stack trace Yellow page(really developer hate this). Through it we can handle error on specific code or we can create a single page for handling all the Error.
When we create a new website it looks like
Here we have mode attribute which can be "On|Off|RemoteOnly"
"RemoteOnly" options is the default option. Specifies that custom errors are shown only to remote clients, and ASP.NET errors are shown to the local host
"On" option shows custom errors are enabled.
"Off" Specifies custom errors are disabled.
We can handle errors based on specific code like that:
< customErrors mode="On" >
< error statusCode="403" redirect="NoAccess.htm" />
< error statusCode="404" redirect="FileNotFound.aspx" />
< error statusCode="500" redirect="GenericErrorPage.htm"/>
or We can use a single page redirect for all the Errors
< customErrors mode="On" defaultRedirect="ForAllError.htm" />
Stay away from Errors now
Keep Happy and Programming:)
1) Page Level
2) Application Level
3) Through Web.Confing
1) Page Level Error Handling :
We can handle error at page level so that if any error comes under the page scope it will handle through the OnError Method of page, But its only at page level not outside that scope. Whenever we create a web form its class drive from System.Web.UI.Page and that why page object help us to invoke page level error. What we do we override OnError method of the page. Like
protected override void OnError(EventArgs e)
{
Exception objException = Server.GetLastError (); // Return Current Exception
string errorDescription =
"Source of Error:" + objException.Source +
"Message : " + objException.Message +
"Stack trace: " + objException.StackTrace;
Response.Write (errorDescription); // Or you can log the Error or redirect to somewhere else/
}
2) Application Level Error Handling :
Another approach for error handling is at application level. Whenever any Error comes Application_Error sub called. Its a good place for handling error because no need to write it on every page. It Works something like that:
void Application_Error(object sender, EventArgs e)
{
Exception objException = Server.GetLastError (); // Return Current Exception
string errorDescription =
"Source of Error: " + objException.Source +
"Message : " + objException.Message +
"Stack trace: " + objException.StackTrace;
// After that use that Error errorDescription as you want eithr make log or redirect to somewhere else/
}
3) Web.Config Custom Errors
Third place is Web.Config, it enables us to configure html error pages to be displayed in place of a error stack trace Yellow page(really developer hate this). Through it we can handle error on specific code or we can create a single page for handling all the Error.
When we create a new website it looks like
Here we have mode attribute which can be "On|Off|RemoteOnly"
"RemoteOnly" options is the default option. Specifies that custom errors are shown only to remote clients, and ASP.NET errors are shown to the local host
"On" option shows custom errors are enabled.
"Off" Specifies custom errors are disabled.
We can handle errors based on specific code like that:
< customErrors mode="On" >
< error statusCode="403" redirect="NoAccess.htm" />
< error statusCode="404" redirect="FileNotFound.aspx" />
< error statusCode="500" redirect="GenericErrorPage.htm"/>
or We can use a single page redirect for all the Errors
< customErrors mode="On" defaultRedirect="ForAllError.htm" />
Stay away from Errors now
Keep Happy and Programming:)
Tuesday, April 14, 2009
Identify Mobile Browser or Iphone using c# Code
Recently i created a mobile browser compatible website for our existing Website.
So as we already marketed our website well for desktop browser but not for Iphone or other Mobile device so we started doing that. Now what client want that whenever any person open our website on mobile device it will redirect to Iphone compatible website
So what, I Just write these lines of code:
if (HttpContext.Current.Request.Browser.IsMobileDevice || HttpContext.Current.Request.UserAgent.ToLower().Contains("iphone"))
{
Response.Redirect("http://iphone.domain.com");
}
on the above code Line
HttpContext.Current.Request.Browser.IsMobileDevice detects the mobile devices but as our Iphone not detected through that so thats why i add one more line :
HttpContext.Current.Request.UserAgent.ToLower().Contains("iphone")
Which detects the Iphone you can use this one for other mobile device not supported by .NET
So as we already marketed our website well for desktop browser but not for Iphone or other Mobile device so we started doing that. Now what client want that whenever any person open our website on mobile device it will redirect to Iphone compatible website
So what, I Just write these lines of code:
if (HttpContext.Current.Request.Browser.IsMobileDevice || HttpContext.Current.Request.UserAgent.ToLower().Contains("iphone"))
{
Response.Redirect("http://iphone.domain.com");
}
on the above code Line
HttpContext.Current.Request.Browser.IsMobileDevice detects the mobile devices but as our Iphone not detected through that so thats why i add one more line :
HttpContext.Current.Request.UserAgent.ToLower().Contains("iphone")
Which detects the Iphone you can use this one for other mobile device not supported by .NET
Import data from Excel to SQl Server
Facing problem while importing table data from Excel to Sql Server.
Some time we have some fields on Excel which contain data which is not supported by
Sql Server like single qote('). I take it just as a example. So i write a program
in c# which collects data from excel table read it make changes on data if we need and insert it to SQL. Problem is basically
that if we have lakhs of record its not possible to edit them manually and then import to sql.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Connection String to Excel Workbook
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourExcelDataSource(like D:\\Detail.xls);Extended Properties=""Excel 8.0;HDR=YES;""";
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand("Select * FROM [Data$]", connection);
connection.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(command);
oda.Fill(ds);
string sqlConnectionString = "Data Source= SqlDataSource; Initial Catalog=DataBaseName;uid=Userid; Password=pwd";
SqlConnection con = new SqlConnection(sqlConnectionString);
con.Open();
for (int c = 0;c < ds.Tables[0].Rows.Count;c++)
{
try
{
DataRow dro = ds.Tables[0].Rows[c];
object[] array = ds.Tables[0].Rows[c].ItemArray; //Break Table to array
string str = "insert into tableName values(";
for (int k = 1; k < array.Length; k++)
{
if (array[k].ToString().Contains("'"))
{
array[k] = array[k].ToString().Replace("'", "''");
}
str = str+"'"+ array[k] + "',";
}
str = str.Substring(0, str.Length - 1);
str = str + ")";
SqlCommand com = new SqlCommand(str);
com.Connection = con;
com.ExecuteNonQuery();
}
catch(Exception ex)
{
continue;
}
}
//}
}
}
}
Some time we have some fields on Excel which contain data which is not supported by
Sql Server like single qote('). I take it just as a example. So i write a program
in c# which collects data from excel table read it make changes on data if we need and insert it to SQL. Problem is basically
that if we have lakhs of record its not possible to edit them manually and then import to sql.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Connection String to Excel Workbook
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourExcelDataSource(like D:\\Detail.xls);Extended Properties=""Excel 8.0;HDR=YES;""";
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand("Select * FROM [Data$]", connection);
connection.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(command);
oda.Fill(ds);
string sqlConnectionString = "Data Source= SqlDataSource; Initial Catalog=DataBaseName;uid=Userid; Password=pwd";
SqlConnection con = new SqlConnection(sqlConnectionString);
con.Open();
for (int c = 0;c < ds.Tables[0].Rows.Count;c++)
{
try
{
DataRow dro = ds.Tables[0].Rows[c];
object[] array = ds.Tables[0].Rows[c].ItemArray; //Break Table to array
string str = "insert into tableName values(";
for (int k = 1; k < array.Length; k++)
{
if (array[k].ToString().Contains("'"))
{
array[k] = array[k].ToString().Replace("'", "''");
}
str = str+"'"+ array[k] + "',";
}
str = str.Substring(0, str.Length - 1);
str = str + ")";
SqlCommand com = new SqlCommand(str);
com.Connection = con;
com.ExecuteNonQuery();
}
catch(Exception ex)
{
continue;
}
}
//}
}
}
}
Validation of viewstate MAC failed / The state information is invalid for this page and might be corrupted Problem
facing a new problem
Validation of viewstate MAC failed / The state information is invalid for this page and might be corrupted
I face this problem long time ago. My websites woriking well at my end in India office and on my client system also. But problem was that it was not working on some system (another office) and they told me that they face some problem (Site through errors) as i use custom error so the error will not display but site was not working. The error we got was
Validation of viewstate MAC failed / The state information is invalid for this page and might be corrupted
So i worked on that and got the answer (after googling). Just add it to your webconfig file: -
pages validateRequest="false" enableEventValidation="false" viewStateEncryptionMode="Never" enableViewStateMac="false" />
and problem resolved
Validation of viewstate MAC failed / The state information is invalid for this page and might be corrupted
I face this problem long time ago. My websites woriking well at my end in India office and on my client system also. But problem was that it was not working on some system (another office) and they told me that they face some problem (Site through errors) as i use custom error so the error will not display but site was not working. The error we got was
Validation of viewstate MAC failed / The state information is invalid for this page and might be corrupted
So i worked on that and got the answer (after googling). Just add it to your webconfig file: -
pages validateRequest="false" enableEventValidation="false" viewStateEncryptionMode="Never" enableViewStateMac="false" />
and problem resolved
IE8 Compatibility Problem
Hi,
Have u in trouble due to not working of your site on IE8 (but Working well on IE7). Here is the
solution (I faced in one of my project).
Just add a HTTP-EQUIV meta tag on your master page if you have or place it on your page
meta equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
and see your website working on IE8 .
Keep Smiling and Programming :)
Have u in trouble due to not working of your site on IE8 (but Working well on IE7). Here is the
solution (I faced in one of my project).
Just add a HTTP-EQUIV meta tag on your master page if you have or place it on your page
meta equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
and see your website working on IE8 .
Keep Smiling and Programming :)
Its all about .Net (Share your Knowldge here)
Hi,
Share your views, Knowldge and problems here
Thanks
Share your views, Knowldge and problems here
Thanks
Subscribe to:
Posts (Atom)