Thursday, February 18, 2010

NET DB Connection class for MySQL

http://snipplr.com/view/24418/net-db-connection-class-for-mysql/


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Odbc;
using System.Configuration;

public class DB_Connect
{
// GLOBAL ACCESS TO COMMAND OBJECT
public static OdbcCommand cmd = new OdbcCommand();
private OdbcDataAdapter _Adapter = new OdbcDataAdapter();
private DataSet _ds = new DataSet();
private DataTable _table = new DataTable();
private OdbcConnection _Conn = new OdbcConnection();
private OdbcDataReader _Reader;


public static object runQuery(string db, bool isProc, bool wantLastID)
{
DataTable DT = new DataTable();
string connStr = null;

//SETS THE CONNECTION STRING
if (db.ToLower() == "MPH_DEV".ToLower())
connStr = "Driver={SQL Server Native Client 10.0};Server=myserver;Database=mydb;Uid=mydb;Pwd=mydb;";
else if (db.ToLower() == "keeneye".ToLower())
connStr = "Driver={MySQL ODBC 5.1 Driver};Server=myserver;Port=3306;Database=mydb;User=myuser;Password=mypass; Option=3;";
else
return null;

//SETTING .NET CONNECTION OBJECTS
OdbcConnection conn = new OdbcConnection(connStr);
OdbcDataAdapter adapter = new OdbcDataAdapter();
DataSet ds = new DataSet();

adapter.SelectCommand = new OdbcCommand();
adapter.SelectCommand = cmd;
adapter.SelectCommand.Connection = conn;

//SPECIFYING IF THIS IS A STORED PROCEDURE
if (isProc)
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
else
adapter.SelectCommand.CommandType = CommandType.Text;

// IF THE USER WANTS THE LAST ID OF THE LAST PREVIOUS INSERT
// OR THEY WANT A DATATABLE OBJECT
if (wantLastID)
{
object rt;
conn.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = "select LAST_INSERT_ID()";
rt = Convert.ToString(cmd.ExecuteScalar());
return rt;
}
else
{
conn.Open();
adapter.Fill(ds, "getData");
DT = ds.Tables["getData"];
conn.Close();
return DT;
}



}

public DB_Connect(string DB_String)
{
_Conn.ConnectionString = ConnString(DB_String);
_Adapter.SelectCommand = new OdbcCommand();
_Adapter.SelectCommand.Connection = _Conn;

}// End Contructor DB_Connect

public DataTable Execute(String table_Name)
{
_Conn.Open();
_Adapter.Fill(_ds, table_Name);
_table = _ds.Tables[table_Name];
_Conn.Close();
return _table;

}// End execute Function

private String ConnString(string DB_Name)
{
// setup your own connection strings via if statements

if (DB_Name == null)
{

return "Nothing";
}

}// Ends the Connection String Method



#region Properties
public OdbcDataAdapter Adapter
{
get { return _Adapter; }
set { _Adapter = value; }
}
public DataSet DS
{

get { return _ds; }
set { _ds = value; }
}
public DataTable DT_Table
{

get { return _table; }
set { _table = value; }
}
public OdbcConnection Connection
{

get { return _Conn; }
set { _Conn = value; }
}
public OdbcDataReader Reader
{

get { return _Reader; }
set { _Reader = value; }
}

#endregion

//EXAMPLE ON HOW TO USE THIS CLASS

//DB_Connect.cmd = new System.Data.Odbc.OdbcCommand();
//DB_Connect.cmd.CommandText = "Insert into tbl_patient_surgical_history(patient_id,procedure_id) values(?,?)";
//DB_Connect.cmd.Parameters.AddWithValue("?patient_id", pt_id);
//DB_Connect.cmd.Parameters.AddWithValue("?procedure_id", item.Value.ToString());
//DB_Connect.runQuery("keeneye", false, false);



}// End Class DB_Connect

XML config files in WinForms

http://snipplr.com/view/24482/persisting-data-using-xml-config-files-in-winforms-saving-and-restoring-user-and-application-data/

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