private void AddOrder(ref DataTable dtb)
{
DataColumn dc = new DataColumn();
dtb.Columns.Add("Order");
for (int i = 1; i <= dtb.Rows.Count; i++)
{
dtb.Rows[i - 1]["Order"] = i;
}
}
Sunday, December 20, 2009
Adding order to Datatable in Ado.net
Wednesday, December 16, 2009
Random String
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace RandomString
{
public static class RandomString
{
static char[] myCharArray = new char[]
{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
,'0','1','2','3','4','5','6','7','8','9'
//,'ก', 'ข', 'ค', 'ง', 'จ', 'ฉ', 'ช', 'ซ', 'ฌ', 'ญ', 'ฎ', 'ฏ', 'ฐ', 'ฑ', 'ฒ', 'ณ', 'ด', 'ต', 'ถ', 'ท', 'ธ', 'น', 'บ', 'ป', 'ผ', 'ฝ', 'พ', 'ฟ', 'ภ', 'ม', 'ย', 'ร', 'ล', 'ว', 'ศ', 'ษ', 'ส', 'ห', 'ฬ', 'อ', 'ฮ'
};
public static string GetRandStringMD5(string hashString)
{
byte[] h = ASCIIEncoding.Default.GetBytes(hashString);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
return System.Text.Encoding.ASCII.GetString(md5.ComputeHash(h));
}
public static string GetRandString(int length)
{
string r = "";
int maxl = myCharArray.Length;
Random rnd = new Random();
for (int i = 0; i < length; i++)
{
int rchar = rnd.Next(0,maxl);
r += myCharArray[rchar];
}
return r;
}
public static string GetRandString(int min, int max)
{
string r = "";
int maxl = myCharArray.Length;
Random rnd = new Random();
int maxr = rnd.Next(min, max);
for (int i = 0; i < maxr; i++)
{
r += myCharArray[rnd.Next(0, maxl)];
}
return r;
}
}
}
Simple Create Child Node in XML
XmlDocument doc = new XmlDocument();
doc.Load("PHP_DataAdapter.xml");
XmlElement _aidRoot = doc.CreateElement("Adapter");
_aidRoot.InnerXml = @"1234 "
+ @""
+ @"";
doc.DocumentElement.AppendChild(_aidRoot);
doc.Save("PHP_DataAdapter.xml");
Tuesday, December 8, 2009
Quick Get DataTable Class
public static class QuickGetDataTable
{
DataTable GetDatatable(string connectionString, string selectCommand) {
SqlDataAdapter sda = new SqlDataAdapter(selectCommand, connectionString);
DataTable dt = null;
sda.Fill(dt);
return dt;
}
}
Wednesday, October 21, 2009
Check Scrollbars Visibility [C#]
Determining the scrollbars visibility
The controls with the auto-scroll functionality provide HorizontalScroll and VerticalScroll properties which enable you to control the automatically shown scrollbars. Use their Visible members to determine whether the scrollbars are visible to the user.
[C#]ScrollableControl;
To demonstrate the properties usage, we will implement a GetVisibleScrollbars method that returns a ScrollBars value specifying which scrollbars are visible.
[C#]using System.Windows.Forms;
protected static ScrollBars GetVisibleScrollbars(ScrollableControl ctl)
{
if (ctl.HorizontalScroll.Visible)
return ctl.VerticalScroll.Visible ? ScrollBars.Both : ScrollBars.Horizontal;
else
return ctl.VerticalScroll.Visible ? ScrollBars.Vertical : ScrollBars.None;
}
An alternative technique
There is an alternative way how to get the scrollbars visibility based on use of the GetWindowLong API function. This technique unlike the foregoing works also on .NET Framework 1.1.
First, create a Win32 class that imports the GetWindowLong function from the user32.dll library and defines some constants.
[C#]
Next, implement the GetVisibleScrollbars method that calls GetWindowLong to get a control's window style and examines the returned value to get the visible scrollbars. Use the control's Handle property as the first argument of the GetWindowLong method.
[C#]using System.Windows.Forms;
protected static ScrollBars GetVisibleScrollbars(Control ctl)
{
int wndStyle = Win32.GetWindowLong(ctl.Handle, Win32.GWL_STYLE);
bool hsVisible = (wndStyle & Win32.WS_HSCROLL) != 0;
bool vsVisible = (wndStyle & Win32.WS_VSCROLL) != 0;
if (hsVisible)
return vsVisible ? ScrollBars.Both : ScrollBars.Horizontal;
else
return vsVisible ? ScrollBars.Vertical : ScrollBars.None;
}
Using the GetVisibleScrollbars method
You can add a VisibleScrollbars property to your control that indicates which scrollbars are displayed in the control window.
A change of scrollbars visibility causes the cotrol's client size change, and thus, firing the Resize event. Handle this event (e.g. by overriding the OnResize method) to detect scrollbars visibility changes.
[C#]Snippet (programming)
Snippet is a programming term for a small region of re-usable source code, machine code or text. Ordinarily, these are formally-defined operative units to incorporate into larger programming modules. Snippets are often used to clarify the meaning of an otherwise "cluttered" function, or to minimize the use of repeated code that is common to other functions.
Snippet management is a feature of some text editors, program source code editors, IDEs, and related software. It allows the user to persist and use snippets in the course of routine edit operations.