Thursday, February 18, 2010

Simple XML parsing using LINQ




The Reddest

Creating an XP Style WPF Button with Silverlight

2/20/2008


The Fattest

Flex And Yahoo Maps

2/12/2007


The Tallest

WPF Tutorial - Creating A Custom Panel Control

2/18/2008



XDocument xmlDoc = XDocument.Load("TestFile.xml");

var tutorials = from tutorial in xmlDoc.Descendants("Tutorial")
select new {
Author = tutorial.Element("Author").Value,
Title = tutorial.Element("Ttle").Value,
Date = tutorial.Element("Date").Value };

Date Taken EXIF Data for a Picture

///
/// Returns the EXIF Image Data of the Date Taken.
///

/// Image (If based on a file use Image.FromFile(f);)
/// Date Taken or Null if Unavailable
public static DateTime? DateTaken(Image getImage)
{
int DateTakenValue = 0x9003; //36867;

if (!getImage.PropertyIdList.Contains(DateTakenValue))
return null;

string dateTakenTag = System.Text.Encoding.ASCII.GetString(getImage.GetPropertyItem(DateTakenValue).Value);

string[] parts = dateTakenTag.Split(':', ' ');
int year = int.Parse(parts[0]);
int month = int.Parse(parts[1]);
int day = int.Parse(parts[2]);
int hour = int.Parse(parts[3]);
int minute = int.Parse(parts[4]);
int second = int.Parse(parts[5]);

return new DateTime(year, month, day, hour, minute, second);
}

The difference between <%= and <%# in ASP.NET

The <%= expressions are evaluated at render time

The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.

The <%# expressions can be used as properties in server-side controls. <%= expressions cannot.















Equals: <%= this.TestValue %>




Pound: <%# this.TestValue %>




Equals label:




Pound label:











//And the code behind is:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
_testValue = "2";
}

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
// DataBind();
_testValue = "3";
}

public string TestValue
{
get { return _testValue; }
}

private string _testValue = "1";
}

Friday, January 15, 2010

Anonymous methods in C#

static void AddUniqe(Item item, List list)
{
int offs = list.FindIndex(delegate(Item i) { return i.id == item.id; });
if (offs >= 0)
list[offs] = item;
else
list.Add(item);
}

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;
}