Take ASP.NET application to offline

Hi All,

Here is a simple way to bring asp.net application down.

Just create a htm file named "app_offline.htm" and place it in your root directory. All request to your website will redirect to that app_offline.htm page automatically. If you keep some text like "Site under maintenance" or if you have some nice information to say the customer that the site is in under construction / maintenance it can be easily conveyed. Once your maintenance work completed, you can just remove that file.

It is not actually a redirect but, ASP.NET essentially shuts down the site, unloads it from the server, and stops processing any requests to your site. Once you delete the app_offline.htm file then everything will work fine and your ASP.NET site will load up and start serving requests.

However, you have to keep in mind one thing that you should have enough content in that file (more than 512 bytes) or IE will consider it as 404 then you will see 404 error message.



 

Difference between DataGrid and GridView

Hi All !

I am back !!!


Difference between DataGrid and GridView:
==========================================


In .Net 2.0, we are using GridView. Since DataGrid and GridView are same we have some extra facilities and features in GridView than DataGrid.

By using GridView we can render it on Mobile Devices also, but DataGrid only use for web pages. Another key difference between DataGrid and GridView controls lies in the adaptive user interface.

In DataGrid paging, sorting, inserting, updating and deleting are implementing by coding. But in GridView automatically generates those things without user writing the code.
Sorting: In DataGrid code requires handling the SortCommand event and rebind grid required. In case of GridView no additional code required.
Paging: In DataGrid requires code to handle the PageIndexChanged event and rebind grid required. In case of GridView no additional code required. It also supports customized appearance.
Data binding: Like GridView DataGrid cannot bind with new datasource control in ASP.NET 2.0.
Updating data: DataGrid requires extensive code to update operation on data. GridView requires little code. Code like exceptions handling for database part.
Events: GridView supports events fired before and after database updates. In DataGrid fewer events supported as compared to GridView.

In DataGrid no image template column, whereas GridView have image template column
The GridView control is the successor to the DataGrid control. Like the DataGrid control, the GridView control was designed to display data in an HTML table. When bound to a data source, the DataGrid and GridView controls each display a row from a DataSource as a row in an output table.

Both the DataGrid and GridView controls are derived from the WebControl class. Although it has a similar object model to that of the DataGrid control, the GridView control also has a number of new features and advantages over the DataGrid control, which include:

Richer design-time capabilities.

Improved data source binding capabilities.

Automatic handling of sorting, paging, updates, and deletes.

Additional column types and design-time column operations.

A Customized pager user interface (UI) with the PagerTemplate property.

Differences between the GridView control and the DataGrid control include:

Different custom-paging support.

Different event models.



...S.VinothkumaR.

How to clear temporary internet foder by using C# in winforms?

Here is the sample code for clearing temporary internet foder by using C# in winforms.

private void btnClear_Click(object sender, EventArgs e)
{
ClearAll(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
MessageBox.Show("Your temporary files has been cleaned.! ");
}

public void ClearAll(DirectoryInfo dirInfo)
{
foreach (FileInfo fileInfo in dirInfo.GetFiles())
{
try
{
fileInfo.Delete();
}
catch { }
}
foreach (DirectoryInfo subDirectory in dirInfo.GetDirectories())
{
ClearAll(subDirectory);
}
}


...S.VinotkumaR.

How to insert bulk values in to a table by using C#?

The below method is used to bulk insert in to a table by using c# from a datatable.

using System.Data;
using System.Data.SqlClient;

public int BulkInsert(DataTable dt, string TableName, string ConnectionString)
{
int returnValue = -1;
SqlConnection oConn = new SqlConnection(ConnectionString);
oConn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM " + TableName,oConn);
da.SelectCommand.Connection = oConn;
DataSet ds = new DataSet();
SqlCommandBuilder oCmd = new SqlCommandBuilder(da);
try
{
da.Fill(ds);
returnValue = da.Update(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
oConn.Close();
oCmd.Dispose();
}
return returnValue;
}


...S.VinothkumaR.

Copy All Directories with Files using C#

Here is the sample code for copying a directory with sub directories and all the files using C#.


public static void CopyAllDirectories(DirectoryInfo source, DirectoryInfo target)
{
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}

foreach (FileInfo fileInfo in source.GetFiles())
{ fileInfo.CopyTo(Path.Combine(target.ToString(),fileInfo.Name), true);
}
foreach (DirectoryInfo sourceSubDir in source.GetDirectories())
{
DirectoryInfo targetSubDir =
target.CreateSubdirectory(sourceSubDir.Name);
CopyAllDirectories (sourceSubDir, targetSubDir);
}
}


...S.VinothkumaR.

Scrollbar in down side of textbox.

Scrollbar in down side of textbox.

Hi all, I have come to give a very simple solution for the below question. The question is

How to set scrollbar in end of the text in textbox when dynamically updating the text value?

Actually I have been developing a big application. On that application I needed to update the status of every action in my application. For the purpose of updating the status, I have used the textbox to show the status with multiline and vertical scrollbar properties. But I could see the vertical bar not going down when updating the status as shown in the below figure which is marked in red.




After googling few minutes I found we have some properties and methods in textboxes of visual studio are SelectionStart and ScrollToCaret method.

By using the above property and method I have found the solution for my question. See the below figure...



Code Snippet:

private void btnUpdate_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtStatus.Text))
txtStatus.Text = txtStatus.Text + Environment.NewLine;
txtStatus.Text = txtStatus.Text + "Status Updated...";

txtStatus.SelectionStart = txtStatus.Text.Length;
txtStatus.ScrollToCaret();
txtStatus.Refresh();
}


...S.VinothkumaR.

Barcode Generation using C#

In this article, I am trying to give you a sample code for generating barcode image by using C#.

We can see in market for all products they are using barcode only. So the barcode is common one. When we try to programming with old language for barcode is very difficult. We needed to go third party component. But in .Net framework it is very easy to generate barcode image.

Barcode font

There is lot of free barcode fonts available in internet. We can search and download it for generating the barcode image. Here is the font which is I am using “Barcodefont.ttf”. We need to download this font and install in your fonts folder which is located in control panel.

For generating barcode image, we need to add the namespaces “System.Drawing, System.Drawing.Imaging” and the file management namespaces.

Sample Code

using System.Drawing.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

public string dirPath = "";

dirPath = Environment.CurrentDirectory;

private void GenerateBarCode()
{
try
{
string path = dirPath + "\\" + DateTime.Now.ToFileTimeUtc().ToString() + ".jpg";
PrivateFontCollection fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(dirPath + "\\BarcodeFont.ttf");
FontFamily fontFamily = new FontFamily("barcode font", fontCollection);
Font font = new Font(fontFamily, 40);

Bitmap bmp = new Bitmap(pbBarcodeImage.Width, pbBarcodeImage.Height);
Graphics graphics = Graphics.FromImage(bmp);
graphics.Clear(Color.White);
SizeF sizeF = graphics.MeasureString(txtEnter.Text.Trim(), font);
Brush brush = new SolidBrush(Color.Black);
graphics.DrawString(txtEnter.Text.Trim(), font, brush, 10, 10);
bmp.Save(path, ImageFormat.Jpeg);
bmp.Dispose();
pbBarcodeImage.Image = new Bitmap(path);
}
catch
{ }
}

Hence we are generating the barcode by using the above code.

...S.VinothkumaR.