Wednesday, July 08, 2009

draw sine wave on Bitmap

string filename = @"C:\0\test.bmp";
int width = 640;

int height = 480;
Bitmap b = new Bitmap(width, height);

for (int i = 0; i < width; i++)
{
int y = (int)((Math.Sin((double)i*2.0*Math.PI/width )+1.0)*(height-1)/2.0);
b.SetPixel(i, y, Color.Black);
}

b.Save(filename);

Sunday, March 15, 2009

Conditional("DEBUG")

#if defined( DEBUG) in C# - use [Conditional("DEBUG")]


System.Diagnostics.Debug uses [Conditional("DEBUG")].

ILDASM.EXE shows that no code is generated.

Copy this code and paste it in your HTML

[Conditional("DEBUG")]
private static void debugtest()
{
...
}

Tuesday, February 10, 2009

Cache

Cache.Insert("KeyValue", someValue, null, DateTime.Now.AddMinutes(15), System.Web.Caching.Cache.NoSlidingExpiration);

Thursday, June 05, 2008

Display a ToolTip for a DataGrid Cell

private void dataGrid1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
try
{
DataGrid.HitTestInfo hti = dataGrid1.HitTest(e.X,e.Y);
if(hti.Type == DataGrid.HitTestType.Cell)
{
toolTip1.SetToolTip(dataGrid1, dataGrid1[hti.Row, hti.Column].ToString());
}
}
catch{}
}

Thursday, May 08, 2008

more SQL

// This example needs the
// System.Data.SqlClient library

#region Building the connection string

string Server = "localhost";
string Username = "my_username";
string Password = "my_password";
string Database = "my_database";

string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;

#endregion


#region Try to establish a connection to the database

SqlConnection SQLConnection = new SqlConnection();

try
{
SQLConnection.ConnectionString = ConnectionString;
SQLConnection.Open();

// You can get the server version
// SQLConnection.ServerVersion
}
catch (Exception Ex)
{
// Try to close the connection
if (SQLConnection != null)
SQLConnection.Dispose();

// Create a (useful) error message
string ErrorMessage = "A error occurred while trying to connect to the server.";
ErrorMessage += Environment.NewLine;
ErrorMessage += Environment.NewLine;
ErrorMessage += Ex.Message;

// Show error message (this = the parent Form object)
MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);

// Stop here
return;
}

#endregion


#region Execute a SQL query

string SQLStatement = "SELECT * FROM ExampleTable";

// Create a SqlDataAdapter to get the results as DataTable
SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(SQLStatement, SQLConnection);

// Create a new DataTable
DataTable dtResult = new DataTable();

// Fill the DataTable with the result of the SQL statement
SQLDataAdapter.Fill(dtResult);

// Loop through all entries
foreach (DataRow drRow in dtResult.Rows)
{
// Show a message box with the content of
// the "Name" column
MessageBox.Show(drRow["Name"].ToString());
}

// We don't need the data adapter any more
SQLDataAdapter.Dispose();

#endregion


#region Close the database link

SQLConnection.Close();
SQLConnection.Dispose();

#endregion

Friday, April 18, 2008

SQLDataReader

public static SqlDataReader Title()
{
SqlConnection connection = ConnectionManager.GetConnection();

var command = new SqlCommand("StoredProcedure", connection);

command.CommandType = CommandType.StoredProcedure;

SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

return reader;
}

Saturday, January 19, 2008

Linear interpolation, C#

static double interpolate( double x0, double y0, double x1, double y1, double x )
{
return y0*(x - x1)/(x0 - x1) + y1*(x - x0)/(x1 - x0);
}

Thursday, April 19, 2007

Country Drop Down List for Web Forms

Monday, March 19, 2007

dropdown states




-- Select --
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
District of Columbia
Delaware
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming


public string SelectedValue
{
get
{
return ddlStates.SelectedValue;
}
set
{
ddlStates.SelectedValue = value;
}
}

*

public string id
{
set
{
ddlStates.ID = value;
}
}

public string ValidationEnabled
{
set
{
rfvStates.Enabled = Convert.ToBoolean(value);
}
}

public string ValidationGroup
{
set
{
ddlStates.ValidationGroup = value;
rfvStates.ValidationGroup = value;
}
}

public string ValidationErrorMessage
{
set
{
rfvStates.ErrorMessage = value;
}
}

public string SelectedValue
{
get
{
return ddlStates.SelectedValue;
}
set
{
ddlStates.SelectedValue = value;
}
}

public short TabIndex
{
set
{
ddlStates.TabIndex = value;
}
}

Thursday, March 08, 2007

state abbreviations list (Dictionary)

Dictionary stateabbrs = new Dictionary();

stateabbrs.Add("Alabama","AL");
stateabbrs.Add("Alaska","AK");
stateabbrs.Add("Arizona","AZ");
stateabbrs.Add("Arkansas","AR");
stateabbrs.Add("California","CA");
stateabbrs.Add("Colorado","CO");
stateabbrs.Add("Connecticut","CT");
stateabbrs.Add("Delaware","DE");
stateabbrs.Add("Florida","FL");
stateabbrs.Add("Georgia","GA");
stateabbrs.Add("Hawaii","HI");
stateabbrs.Add("Idaho","ID");
stateabbrs.Add("Illinois","IL");
stateabbrs.Add("Indiana","IN");
stateabbrs.Add("Iowa","IA");
stateabbrs.Add("Kansas","KS");
stateabbrs.Add("Kentucky","KY");
stateabbrs.Add("Louisiana","LA");
stateabbrs.Add("Maine","ME");
stateabbrs.Add("Maryland","MD");
stateabbrs.Add("Massachusetts","MA");
stateabbrs.Add("Michigan","MI");
stateabbrs.Add("Minnesota","MN");
stateabbrs.Add("Mississippi","MS");
stateabbrs.Add("Missouri","MO");
stateabbrs.Add("Montana","MT");
stateabbrs.Add("Nebraska","NE");
stateabbrs.Add("Nevada","NV");
stateabbrs.Add("New Hampshire","NH");
stateabbrs.Add("New Jersey","NJ");
stateabbrs.Add("New Mexico","NM");
stateabbrs.Add("New York","NY");
stateabbrs.Add("North Carolina","NC");
stateabbrs.Add("North Dakota","ND");
stateabbrs.Add("Ohio","OH");
stateabbrs.Add("Oklahoma","OK");
stateabbrs.Add("Oregon","OR");
stateabbrs.Add("Pennsylvania","PA");
stateabbrs.Add("Rhode Island","RI");
stateabbrs.Add("South Carolina","SC");
stateabbrs.Add("South Dakota","SD");
stateabbrs.Add("Tennessee","TN");
stateabbrs.Add("Texas","TX");
stateabbrs.Add("Utah","UT");
stateabbrs.Add("Vermont","VT");
stateabbrs.Add("Virginia","VA");
stateabbrs.Add("Washington","WA");
stateabbrs.Add("West Virginia","WV");
stateabbrs.Add("Wisconsin","WI");
stateabbrs.Add("Wyoming","WY");