I had a requirement recently to run my little windows service every one minute or an interval thats configurable....
And Timers was straight forward solution for me...
Event on which you need to write your code is OnElapsedTime.
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
streamWriter = new StreamWriter(new FileStream(
"C:\\WindowsServiceLogger.txt", System.IO.FileMode.Append));
this.streamWriter.WriteLine(" My Service got invoked again at " +
DateTime.Now.ToString());
this.streamWriter.Flush();
this.streamWriter.Close();
Start();
}
My task for the service is written in Start() method so i can call it directly OnStart and OnElapsedTime
Please remember to enable your timer OnStart and disable OnStop.
Seems all simple and easy....and it is :)
Happy Coding
Monday, September 10, 2007
Export to Excel from pagination enabled Gridview
Recently couple of developers asked me this question, so thought of pinning it down here....
very simple way is to turn of your gridview pagination and bind it. As you must be aware use HTMLWriter to render you control.
................
htmlWrite.RenderBeginTag(HtmlTextWriterTag.Tr);
GridView4.AllowPaging = false;
GridView4.DataBind();
GridView4.RenderControl(htmlWrite);
htmlWrite.Write(" Header of my GridView");
htmlWrite.RenderEndTag();
htmlWrite.Write("
");
...........................
to make your excel look cool add/modify the htmlwriter settings....n e questions shoot me a mail :)
very simple way is to turn of your gridview pagination and bind it. As you must be aware use HTMLWriter to render you control.
................
htmlWrite.RenderBeginTag(HtmlTextWriterTag.Tr);
GridView4.AllowPaging = false;
GridView4.DataBind();
GridView4.RenderControl(htmlWrite);
htmlWrite.Write(" Header of my GridView");
htmlWrite.RenderEndTag();
htmlWrite.Write("
");
...........................
to make your excel look cool add/modify the htmlwriter settings....n e questions shoot me a mail :)
Tuesday, July 3, 2007
ObjectDataSource - A powerfull datasource control
A middle tier two-way talking control, thats what you can say about this datasource control. Suitable when you have a requirement of encapsulating your buisness logic in object, hence we say this control supports three tier architecture by providing a way to bind your presentation layer and data tier.
The ObjectDataSource control exposes a TypeName property that specifies an object type (class name) to instantiate for performing data operations. You can bind this control to a DAL (data access layer) or BLL (business logic layer) depending on requirement. More secured and organised is to BLL, thus return data will be stateless.
Nice tutorial for begineers is here : http://quickstart.developerfusion.co.uk/QuickStart/aspnet/doc/ctrlref/data/objectdatasource.aspx
The ObjectDataSource control exposes a TypeName property that specifies an object type (class name) to instantiate for performing data operations. You can bind this control to a DAL (data access layer) or BLL (business logic layer) depending on requirement. More secured and organised is to BLL, thus return data will be stateless.
Nice tutorial for begineers is here : http://quickstart.developerfusion.co.uk/QuickStart/aspnet/doc/ctrlref/data/objectdatasource.aspx
Labels:
BLL,
DAL,
ObjectDataSource,
three tier architecture
Monday, July 2, 2007
Smart ways to add your database connection
Now a small question, where will you add the connection string for your project?
Simple question, right. There are multiple options here, you may place this in your code, config file, etc. Lets explore this in detail.
Its a bad idea to store connection strings in your pages for different reasons.
1. Bad practice from security point of view. In theory, no one should ever be able to view the source code of your ASP.NET pages. In practice, however, hackers have discovered security flaws in the ASP.NET framework. To sleep better at night, you should store your connection strings in a separate file.
2. Difficult managing change requests. Adding a connection string to every page makes it difficult to manage a website. If you ever need to change your password, then you need to change every page that contains it. If, on the other hand, you store the connection string in one file, you can update the password by modifying the single file.
3. Performance, connection pooling which is auto done by ADO.NET checks character by character and strings in multiple places will hence affect performance.
So strings in configuration files is our option here.Now that is a smart option, and an even more smart option if you need multiple application to access the same conneciton string is to place the same in the root config file (C:\WINDOWS\Microsoft.NET\Framework\[version]\CONFIG folder)
Having said that, lets just try to make this a bit more secure by encrypting the connection string. You may use aspnet_regiis to encrypt your config file as below
aspnet_regiis -pef connectionStrings ""
-pef (Protect Encrypt Filepath)
Simple question, right. There are multiple options here, you may place this in your code, config file, etc. Lets explore this in detail.
Its a bad idea to store connection strings in your pages for different reasons.
1. Bad practice from security point of view. In theory, no one should ever be able to view the source code of your ASP.NET pages. In practice, however, hackers have discovered security flaws in the ASP.NET framework. To sleep better at night, you should store your connection strings in a separate file.
2. Difficult managing change requests. Adding a connection string to every page makes it difficult to manage a website. If you ever need to change your password, then you need to change every page that contains it. If, on the other hand, you store the connection string in one file, you can update the password by modifying the single file.
3. Performance, connection pooling which is auto done by ADO.NET checks character by character and strings in multiple places will hence affect performance.
So strings in configuration files is our option here.Now that is a smart option, and an even more smart option if you need multiple application to access the same conneciton string is to place the same in the root config file (C:\WINDOWS\Microsoft.NET\Framework\[version]\CONFIG folder)
Having said that, lets just try to make this a bit more secure by encrypting the connection string. You may use aspnet_regiis to encrypt your config file as below
aspnet_regiis -pef connectionStrings "
-pef (Protect Encrypt Filepath)
Subscribe to:
Posts (Atom)