Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Pocket PC Personal MP3 Disc Catalogue Search Tool

0.00/5 (No votes)
21 Sep 2005 1  
Windows Mobile application to search MP3 music catalogue
Sample Image - PocketCatalogue.gif

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:

// This code is in the frmMain_Load event of the frmMain class
PocketCatalogue.Database myDB;
myDB = new Database(PathToDatabase,DatabaseName);
myDB.CreateDB();

// This code is in the Database class
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:

// This code is in the ConnectivityTest class
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:

// This code is in the Util class
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);
                //cWidths.Add(sf.Width);
                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:

// This code is in the tabMain_SelectedIndexChanged event of the frmMain class
PocketCatalogue.SyncService.MusicCatalogueSync wsSync =
		new PocketCatalogue.SyncService.MusicCatalogueSync();
AsyncCallback cbDisc = new AsyncCallback(ServiceCallback_DiscCount);
wsSync.BeginGetDiscCount(cbDisc, wsSync);

// This code is in the frmMain class
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here