Introduction
This application is essentially a search tool for finding what Disc Number the Music Album I want to listen to is on. This application also contains functionality to download its data from the central Database server (via a WebService).
I'm still actively working on this project, so please give me your constructive comments and design suggestions. Right now, this app is very specialized for my needs, but if there is enough interest (and good design direction ideas) I could easily be persuaded to update and morph the project.
Framework note
Special note: Thanks to the following article on CodeProject. It helped me a lot (plus I sampled parts of their code).
Background
I have an extensive collection of MP3s that I sort by Album folder and burn onto CD-R for use in my car stereo. Once I got to 100 discs (Over 900 albums) I realized that I needed a better way of finding the music I wanted to listen to. So I created a MySQL database on my server and a search tool on my Pocket PC.
Snippets
Part of the code used to create the database that is used on the Pocket PC:
PocketCatalogue.Database myDB;
myDB = new Database(PathToDatabase,DatabaseName);
myDB.CreateDB();
public void CreateDB() {
if (!System.IO.File.Exists(_pathToDatabase + _databaseName)) {
SqlCeEngine engine;
engine = new SqlCeEngine("Data Source = " + _pathToDatabase + _databaseName);
engine.CreateDatabase();
engine = null;
CreateTables();
}
}
public void CreateTables(){
if (conn == null) {GetConnected();conn.Open(); }
try {
string SQL = "CREATE TABLE Albums ( " +
"DiscID int, " +
"AlbumID int, " +
"ArtistName nvarchar(254), " +
"AlbumName nvarchar(254), " +
"Genre nvarchar(45) )";
SqlCeCommand createTable = conn.CreateCommand();
createTable.CommandText = SQL;
createTable.ExecuteNonQuery();
SQL = "CREATE TABLE Discs ( " +
"DiscID int, " +
"DiscNumber int, " +
"DiscName nvarchar(45), " +
"HasLabel int )";
createTable = conn.CreateCommand();
createTable.CommandText = SQL;
createTable.ExecuteNonQuery();
} catch (System.Data.SqlServerCe.SqlCeException ex) {
MessageBox.Show(ex.Message, "DB Error");
}
}
private void GetConnected(){
conn = new SqlCeConnection("Data Source = " + _pathToDatabase + _databaseName);
}
Part of the code used to check for network connectivity before enabling the WebService
calls:
public bool IsWebAccessible() {
HttpWebRequest hwrRequest;
HttpWebResponse hwrResponse;
string strUrl = @"http://www.microsoft.com/";
bool bConnected = false;
try{
hwrRequest = (HttpWebRequest)WebRequest.Create(strUrl);
hwrRequest.Timeout = 10;
hwrResponse = (HttpWebResponse)hwrRequest.GetResponse();
if(hwrResponse.StatusCode == HttpStatusCode.OK){
bConnected = true;
}
}catch(WebException we){
bConnected = false;
}catch(Exception ex){
bConnected = false;
}finally{
hwrRequest = null;
hwrResponse = null;
}
return bConnected;
}
Method used to make the DataGrid
pretty and autosized:
public static void FormatDataGrid(DataGrid dg, DataSet ds) {
Font f2 = new Font("Tahoma",7, System.Drawing.FontStyle.Regular);
dg.Font = f2;
if (ds != null) {
if (dg.TableStyles.Count > 0)
dg.TableStyles.Clear();
float sumWidths = 0.0f;
foreach (DataTable dt in ds.Tables) {
DataGridTableStyle DGStyle = new DataGridTableStyle();
DGStyle.MappingName = dt.TableName;
DataGridTextBoxColumn textColumn;
System.Collections.ArrayList cWidths;
cWidths = new System.Collections.ArrayList();
Graphics g = dg.CreateGraphics();
Font f = dg.Font;
SizeF sf;
int y = 0;
foreach (DataColumn dc in dt.Columns) {
sf = g.MeasureString("W" + dc.Caption, f);
if (y == 0)
cWidths.Add(36);
else
cWidths.Add(102);
y++;
}
foreach (DataColumn dc in dt.Columns) {
textColumn = new DataGridTextBoxColumn();
textColumn.MappingName = dc.ColumnName;
textColumn.HeaderText = "" + dc.Caption;
textColumn.Width =
Convert.ToInt32(Convert.ToSingle(cWidths[dc.Ordinal]));
DGStyle.GridColumnStyles.Add(textColumn);
sumWidths += textColumn.Width;
}
dg.TableStyles.Add(DGStyle);
}
}
}
Example of one of the many asynchronous WebService
calls:
PocketCatalogue.SyncService.MusicCatalogueSync wsSync =
new PocketCatalogue.SyncService.MusicCatalogueSync();
AsyncCallback cbDisc = new AsyncCallback(ServiceCallback_DiscCount);
wsSync.BeginGetDiscCount(cbDisc, wsSync);
private void ServiceCallback_DiscCount(IAsyncResult ar) {
try {
PocketCatalogue.SyncService.MusicCatalogueSync wsSync =
(PocketCatalogue.SyncService.MusicCatalogueSync)ar.AsyncState;
this.lblDiscsServer.Text = wsSync.EndGetDiscCount(ar).ToString();
bDiscCountReturned = true;
if ((bDiscCountReturned) && (bAlbumCountReturned) && (bTrackCountReturned)) {
this.cmdSync.Enabled = true;
this.lblStatusMessage.Text = "";
tmrProgress.Enabled = false;
}
} catch (Exception) {
MessageBox.Show("Error terminating Web Service call", this.Text);
tmrProgress.Enabled = false;
}
}
Points of Interest
This was my first Pocket PC Application and I really enjoyed it, and I plan on doing many more. I learned lots of things from this little project including threading on a mobile device, SqlCE database technologies and Asynchronous WebService access.
History
- Version 1.0, September 21 2005
Coming Soon
- Version 1.1 will include the create statements necessary for making the corresponding Server MySQL Database
- Version 1.2 will include the code for the
WebService
being used