RSS

Working with Culture Info

How to create the Culture Info for Date Time Format Provider

CultureInfo represents information about a specific culture including the names of the culture, the writing system, and the calendar used, as well as access to culture-specific objects that provide information for common operations, such as formatting dates and sorting strings.


The CultureInfo class holds culture-specific information, such as the associated language, sublanguage, country/region, calendar, and cultural conventions. This class also provides access to culture-specific instances of DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo. These objects contain the information required for culture-specific operations, such as casing, formatting dates and numbers, and comparing strings.


Assembly: Mscorlib (in Mscorlib.dll)
Namespace: System.Globalization

using System.Globalization;

To Create Object for CultureInfo:
CultureInfo Culture = new CultureInfo("en-Us");
DateTime Dt=Convert.ToDateTime("01/01/1984",Culture);

-> Culture For Tamil India -> ta-IN , ta
- For More About CultureInfoVisit MSDN

read comments Read User's Comments

How to Use Background Worker to Update the UI or Win Form Controls

About BackgroundWorker Process

When we have a function that takes a long time and it freezes the screen, the user gets nervous. They simply don't like to lose the control. It would give a friendly impression when they can still click on the form while the application is busy with getting the information.



For example, when you start implementing asynchronous methods, you will wonder which technique is best suitable for your application. Would you use a Timer class or a Thread class or a Background Worker? This post uses a BackgroundWorker class to show how easy it is, and where we need to pay attention when using it.



- There are three event handler methods are available to call the method asynchronously.
1. DoWork Event Handler
The DoWork method is like any other event handler.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Very time-consuming process or method.
}

2.RunWorkerCompleted Method
This event handler is used to display the status of the worker process after completion. So we can use for to display “Success” or “Failed” of the Process.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (Convert.ToBoolean(e.Result) == true)
MessageBox.Show("Process List Loaded Successfully!");
else
MessageBox.Show("Process List Load Failed!");

}

3.Progress Changed Method
This is used to display the status of the process. For example we will have a Progress bar and each and every step increase the Progress status.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e){  }

Some of other important Keys are:

1.e.Argument – Used to get the parameter reference received by RunWorkerAsync.
2.e.Result - Check to see what the BackgroundWorker processing did
3.backgroundWorker1.RunWorkerAsync(object)
Called to start a process on the worker thread.
4.DoWorkEventArgs e :
Contains e.Argument and e.Result, so it is used to access those properties.

How to Accessing the Win Forms Controls form the Background worker:

- We can access through the anonymous methods of C#. The bellow example shows how to access the win forms combo box control through the background worker process.
- In this example retrieve the current process name list available of your system.



- So first add the namespace to Process, Thread class
using System.Diagnostics;
using System.Threading;

- To call background worker’s RunWorkerAsync() method
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}

- Next Background worker’s DoWorker Method
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// To call method directly
// bflg = LoadCombo();
// To call Using delegates
ToLoadCombo Tocall = new ToLoadCombo(LoadCombo);
e.Result = Tocall();
}

- Then Next Step we need to include LoadCombo()
//Public Method For Accessing Form Control (i.e CmbProcess)
public bool LoadCombo()
{
//Here is a simple way to update any UI controls from worker threads
// using anonymous methods and local variable capture.
try
{
new Thread(delegate() // anonymous methods
{
this.Invoke((ThreadStart)delegate() // anonymous methods Call
{
foreach (Process item in Process.GetProcesses())
{
cmbProcess.Items.Add(item.ProcessName.ToString());
}
cmbProcess.SelectedIndex = 0;
});
}).Start();
}
catch
{
return false;
}
return true;
}

- At Last we need to add RunWorkerCompleted Event Handler

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (Convert.ToBoolean(e.Result) == true)
MessageBox.Show("Process List Loaded Successfully!");
else
MessageBox.Show("Process List Load Failed!");

}

Using the above code we will develop faster and freezes the screen Application.

read comments Read User's Comments

How to Generate Unique ID using GUID in C#

We can Generate the Unique ID or number for our Web User as Member ID. And also we can update the unique records of the Database using GUID of the C#.

>> The GUID is used to Generate the unique id's. It is pronounced as goo'id - Globally unique identifier.

>> It is a 128-bit integer that can be used to uniquely identify something. You may store users or products in your database and you want somehow uniquely identify each row in the database. A common approach is to create a autoincrementing integer, another way would be to create a GUID for your products.

>> How to Create ID

- System.GUID class is used to create a Rand Id for members.

-> Using this bellow code we can create

System.Guid mid = Guid.NewGuid();
MessageBox.Show(mid.ToString());

or
 MessageBox.Show(System.Guid.NewGuid().ToString("P"));

Some of Formats: P, N, D,B
Ex.
MessageBox.Show("This is Test ID:" + System.Guid.NewGuid().ToString("P"));
MessageBox.Show("This is Test ID:" + System.Guid.NewGuid().ToString("D"));
MessageBox.Show("This is Test ID:" + System.Guid.NewGuid().ToString("B"));

read comments Read User's Comments

Date Time conversion Formats in SQL Server

Date Time conversion Format

DATE FORMATS


Format # Query (current date: 12/30/2006) Sample
1 select convert(varchar, getdate(), 1) 12/30/06
2 select convert(varchar, getdate(), 2) 06.12.30
3 select convert(varchar, getdate(), 3) 30/12/06
4 select convert(varchar, getdate(), 4) 30.12.06
5 select convert(varchar, getdate(), 5) 30-12-06
6 select convert(varchar, getdate(), 6) 30 Dec 06
7 select convert(varchar, getdate(), 7) Dec 30, 06
10 select convert(varchar, getdate(), 10) 12-30-06
11 select convert(varchar, getdate(), 11) 06/12/30
101 select convert(varchar, getdate(), 101) 12/30/2006
102 select convert(varchar, getdate(), 102) 2006.12.30
103 select convert(varchar, getdate(), 103) 30/12/2006
104 select convert(varchar, getdate(), 104) 30.12.2006
105 select convert(varchar, getdate(), 105) 30-12-2006
106 select convert(varchar, getdate(), 106) 30 Dec 2006
107 select convert(varchar, getdate(), 107) Dec 30, 2006
110 select convert(varchar, getdate(), 110) 12-30-2006
111 select convert(varchar, getdate(), 111) 2006/12/30

read comments Read User's Comments

How to Use PropertyGrid

How to use PropertyGrid in our C# Application

- The Propertygrid is like our Property window available in the .Net IDE. We can also add the propertygrid in C# window application

-The property grid is a powerful control for allowing users to edit the internals of your published classes.Because of the ability for the property grid to reflect and bind to your class, there is not much work involved in getting the property grid up and working.You can add categories and descriptions to your property grid by using the special grid attributes in the System.Component model.

-It displays the Properties and attributes with it's Description and the Category wise details of Object.

- All properties should have get and set methods.(If you don't have a get method, the property won't show up in the PropertyGrid).




- The Bellow attributes are used to manipulate the Properties of the class

1.CategoryAttribute - This will Arrange the Properties as a Category wise.
2.DescriptionAttribute - This will used for description of the Property
3.BrowsableAttribute -
This is used to determine whether or not the property is shown or hidden in
the property grid.
4.ReadOnlyAttribute -
Use this attribute to make your property read only inside the property grid
5.DefaultValueAttribute -
Specifies the default value of the property shown in the property grid
6.DefaultPropertyAttribute -
If placed above a property, this property gets the focus when the property
grid is first launched. Unlike the other attributes, this attribute goes
above the class.
The Bellow cust class object is used to load in PropertyGrid

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace ExampleForTabControl
{
[DefaultPropertyAttribute("ID Settings")]
class Cust
{
private string _name;
private int _age;
private DateTime _dob;
private string _address;
private string _email;
private bool _freq;

[CategoryAttribute("ID Settings"), DescriptionAttribute("Name of the customer") ]
public string Name
{
get {return _name; }
set { _name = value; }
}
[CategoryAttribute("Age Details"), DescriptionAttribute("DOB Details")]
public DateTime DOB
{
get { return _dob; }
set { _dob = Convert.ToDateTime(value); }
}
[CategoryAttribute("Age Details"), DescriptionAttribute("Age in No of Years")]
public int Age
{
get { return _age; }
set { _age = Convert.ToInt32(value); }

}

[CategoryAttribute("Addressing Settings"), DescriptionAttribute("Customers Address")]
public string Address
{
get { return _address; }
set { _address = value; }
}
[CategoryAttribute("Marketting Settings"), DescriptionAttribute("Most current e-mail of the customer")]
public string Email
{
get { return _email; }
set { _email = value; }
}
[CategoryAttribute("Buying Status"), DescriptionAttribute("Frequently Buying Status")]
public bool FrequentBuyer
{
get { return _freq; }
set { _freq =Convert.ToBoolean(value); }
}
}
}


To Bind with PropertyGrid available in the Form1:
private void Form1_Load(object sender, EventArgs e)
{
tabEx1.Visible = false;
Cust Siva = new Cust();
Siva.Name = "Raja";
Siva.Age = 55;
Siva.DOB = Convert.ToDateTime("10-Feb-1985");
Siva.Email = "Siva@gmail.com";
Siva.FrequentBuyer = true;
Siva.Address = "Pallikkaranai,Chennai";
this.proGridCust.SelectedObject = Siva;
this.Text = "Customer";
}

* To download the Source code for the PropetyGrid Example, Please Click Here

read comments Read User's Comments