25 November 2008

C# and ASP.NET Create Table Dynamically

In the run-time sometimes we have to create table dynamically.

From the namespace System.Data DataTable DataColumn classes we can easily create table dynamically in C# and also in ASP.NET.
using System.Data;

// Create a DataTable instance
DataTable dTbl = new DataTable("myDynamicTable");

// Create a DataColumn instances

DataColumn dValue = new DataColumn();
DataColumn dMember = new DataColumn();

dValue.ColumnName = "Id";
dValue.DataType = Type.GetType("System.Int32");

dMember.ColumnName = "Name";
dMember.DataType = Type.GetType("System.String");

// Add these DataColumns into the DataTable

dTbl.Columns.Add(dValue);
dTbl.Columns.Add(dMember);
After you create table dynamically, you can add rows into it.
It is a good way to create DataRow from the table we create, with the function NewRow().
// Create a DataRow Instance from the table we create above, with NewRow();

DataRow myrow = dTbl.NewRow();

myrow["Id"] = 1;
myrow["Name"] = "Tux";

// Add the row into the table

dTbl.Rows.Add(myrow);
That's all for creating table dynamically in C# and also ASP.NET.

Meaning of De parvis grandis acervus erit

When you open the Google toolbar about box, you may see this text there.
De parvis grandis acervus erit.


After I googling, I found several possible meanings;
  • Out of small things a great heap will be formed.
  • Little by little there's so much done.
  • Tall oaks from little acorns grow.
  • Small things will make a large pile.
I really love this quote, I think it summarize the Google great services clearly, their passion and simplicity beyond the great work and service. Please let me know if this translations are incorrect or any other suggestions.

C# Process Process.GetProcessesByName, Kill Process and Exit Event

A process instance can be created by call Process.GetProcessesByName with the name of the process.
Let's say we want to reach, notepad.exe.
  • Get the process by its name
using System.Diagnostics;

private Process GetaProcess(string processname)
{
Process[] aProc = Process.GetProcessesByName(processname);

if (aProc.Length > 0)
return aProc[0];

else return null;
}
Call GetaProcess("notepad.exe"); will return notepad process, if it is already launched.
  • To Kill the process
Process myprc = Call GetaProcess("notepad.exe"); myprc.Kill();
Will kill the process notepad.exe.
  • Handle the Exit Event of the Process
 Process myprc = Call GetaProcess("notepad.exe");
myprc.Exited += new EventHandler(myprc_Exited);

void myprc_Exited(object sender, EventArgs e)
{
MessageBox.Show (((Process)sender).ProcessName + " process has exited!");
}
Now we will noticed when the notepad process exited.

C# Illegal Cross Thread Operation Problem Solution

In Visual Studio, using .NET Framework,
if you are trying to access a user interface control, in multi thread operation, you may get this error Illegal Cross Thread Operation .

That is the thread other than the thread it was created on has no access to change the control interface properties.
To avoid the Illegal Cross Thread Operation error here is the solution;
  • Make a delegate for the control which you want update in multi threading.
  • Check if needs InvokeRequired.
  • Invoke the control, then it will invoke ones again, by its creator object.
  • Second time the control invokes, set its properties as you wish.
The Following C# sample code shows all these steps.
public delegate void UpdateMeLabel(string _str, Color _fontcolor);
public void FormTxtUpdate(string str, Color fontcolor)
{
if (this.label1.InvokeRequired)
{
UpdateMeLabel updaterdelegate = new UpdateMeLabel(FormTxtUpdate);
this.Invoke(updaterdelegate , new object[] { str, fontcolor });
}
else
{
label1.Text = str;
label1.ForeColor = fontcolor;
}

}
By this way, we update the control label1, its text and color values. For example call FormTxtUpdate("Hello World", Color.Red) in multi thread operation, Then label1 text will set to "Hello World" and its color set to Red.

C# Event Log Write To EventLog Entry

Write to event log in C#
  • Add reference using System.Diagnostics to your project
  • Check whether the source exits or not.
  • If not create event source.
  • Create an EventLog object.
  • Set its event source.
  • Write the entry into event source.
C# sample source code is below;

using System.Diagnostics;

private void WriteToEventLog(string message)
{
string cs = "Your Source Name";
EventLog elog = new EventLog();

if (!EventLog.SourceExists(cs))
{
EventLog.CreateEventSource(cs, cs);
}

elog.Source = cs;
elog.EnableRaisingEvents = true;
elog.WriteEntry(message);
}

C# Event Log Write To EventLog Entry tutorial

Fan-out

Fan-out is the number of loads that the output of a gate can drive without decreasing the performance of the logic gate.
Fan-out depends on the logic family of related gate.

Fan-in

Fan-in is the number of inputs available on a logic gate.

Fan-in and gate

Fan in is 2 in the and gate diagram above.
There are 2 inputs on this gate.