Thursday, November 20, 2008

Wednesday, November 12, 2008

Tuesday, November 11, 2008

Part V: Web Pages

Now that we have the classes ready let's create the web pages and finish up this project.

We'll start with the categories page, create a new web form and uncheck the "Place code in separate file" check box, call it "Categories.aspx"

The page has a grid view control and an object data source control. The grid view has template fields for viewing, editing and adding items. The object data source points to the category class and its methods.

The view and edit are handled by the object data source and grid view but he insert command needs some extra code. Here the code to add a new category:

<script runat="server">
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TextBox title = grdCategories.FooterRow.FindControl("txtTitleAdd") as TextBox;

        odsCategories.InsertParameters["title"].DefaultValue = title.Text;
        odsCategories.Insert();
    }
</script>

I have the text field at the footer of the grid view so the first line creates a new field pointing to the grid view field, the second line gets the field text value and the third line inserts the category into the table.

The same concept is applied to the bookmarks page. Create a new web form, uncheck the box and call it "Default.aspx", calling it default to be the startup page.

Here's the add button code:

protected void btnAdd_Click(object sender, EventArgs e)
{
     TextBox title = grdBookmarks.FooterRow.FindControl("txtTitleAdd") as TextBox;
     TextBox url = grdBookmarks.FooterRow.FindControl("txtUrlAdd") as TextBox;
     DropDownList categoryId = grdBookmarks.FooterRow.FindControl("ddlCategoriesAdd") as DropDownList;

     odsBookmarks.InsertParameters["title"].DefaultValue = title.Text;
     odsBookmarks.InsertParameters["url"].DefaultValue = url.Text;
     odsBookmarks.InsertParameters["categoryId"].DefaultValue = categoryId.SelectedValue;
     odsBookmarks.Insert();
}

A bookmark has a title, url and a category id. The first three lines create the fields we need to get the values of the new bookmark. The next three lines sets the parameter values and the last line insert the record into the bookmark table.

The complete code can be found here:
Categories.aspx
Default.aspx

If the code doesn't show up then view the source code of the page.

I didn't get into the grid view asp and html code because the main goal of this tutorial is how to use .NET with MySQL. I hope this tutorial will help get started. Have a look at the grid view code, it's quite interesting what you can do with this powerful control.

Monday, November 10, 2008

Family Guy - Visiting Ground Zero

Going Ergonomic

Microsoft Comfort Curve 2000

A few months ago I bought the Microsoft Comfort Curve 2000 Keyboard (wired), it’s not ergonomic but halfway there. The keys are curved and very thin similar to a laptop keyboard but better. The reason for buying it is my wrists, felt a sharp pain and thought of buying a new keyboard but didn’t want to spend a lot of money so I got this one from Futureshop.ca for $15 CAD. Very cheap compared to other ergonomic or “somewhat ergonomic” models. If you looking for a cheap keyboard and one that you don’t have to spend time getting used to then the Microsoft Comfort Curve 2000 Keyboard is the one.

Microsoft Comfort Curve 2000

Pros: cheap, thin keys, curved, easy to get used to

Cons: no customizable keys


Logitech TrackMan Wheel

Last week I felt a pain in my right wrist from using the mouse for long hours so I bought the Logitech TrackMan Wheel (wired) from

Logitech TrackMan Wheel

Newegg.ca for $36 CAD with free shipping. This mouse is the best mouse I’ve ever used. In a few minutes I was quite comfortable using it, looks like it’ll take awhile to get to use your thumb and not your hand but not at all.

Pros: comfortable, easy to use, doesn't take a lot of desk space

Cons: only three buttons (two buttons and scroll), no customizable buttons, not for games (I use a regular mouse when playing games)


Microsoft Ergonomic Keyboard 4000

This is a true ergonomic keyboard. The first keyboard I've ever had was the white split in half Microsoft keyboard but unfortunately I started using regular keyboards when I was in university and now at work. Today I bought the 4000 version which is obviously improved and even more comfortable. Bought the Microsoft Ergonomic Keyboard 4000 from Staples.ca for $50 CAD, expensive but worth it. Been using it for a few hours and so far so good, no doubt I've made a number of misspellings but that's expected.

Microsoft Ergonomic Keyboard 4000

Pros: ergonomic, customizable buttons, keys for using calculator (brackets, backspace, equal), function keys, integrated cushion

Cons: spacebar is hard to press (maybe just my keyboard), cheap made (my old regular Logitech keyboard is better made than this one!), takes awhile to get used to, big! 19.8" ~ 50cm wide
 

Conclusion

If you're a programmer like me or one who spends long hours using  the keyboard and mouse then I would definitely recommend using the above devices.

Which keyboard to get? Well, I spend more time at work than home so I'm probably going to use the 4000 at work and the 2000 at home. If you don't want to spend $50 for a keyboard then the 2000 is the answer.

What about the mouse? I only have one trackball mouse and I think I'll use it at work and get another one for home. I might just get the Logitech Marble Mouse for a change but in any case my next mouse will be a trackball mouse for sure. You've got to try a trackball mouse.

Hope this post will help you choose your next keyboard and/or mouse. I'll post picture of the keyboards and mouse from different angles.

Feel free to comment about the above devices or other devices, I'm always on the lookout for a better more comfortable mouse and keyboard.

Monday, November 3, 2008

Part IV: MySQL and .NET: Classes

We are going to create two classes, Bookmark and Category, which represent the two tables.

First, If you don't have the “App_Code” folder in your project already, right click on the project > Add ASP.NET Folder > App_Code.

Now add two classes to App_Code, Bookmark and Category, and add the following lines to use the DataTable and MySQL classes:

using System.Data;
using MySql.Data.MySqlClient;

The classes are somewhat similar, every function connects to the database server, sets the stored procedure parameter, performs the operation and returns data in the case of a select statement.

Let's look at one common piece of code line by line:

// create a MySQL connection object and
// set the connection string
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["DBMonkey"].ToString());

// create a MySQL command to perform the SQL operations

MySqlCommand cmd = new MySqlCommand();

try

    // set the command's connection object
    cmd.Connection = conn; 

    // set the name of the stored procedure
    cmd.CommandText = "categoryDelete"; 

   
   // tell the command that it will execute a stored procedure
    cmd.CommandType = CommandType.StoredProcedure; 

    // add a MySQL parameter object to the command and
    // set the parameter name and value
    cmd.Parameters.Add(new MySqlParameter("in_categoryId", categoryId));

    // open the connection
    conn.Open();

    // execute the command
    cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
    throw new Exception(ex.Message);
}
finally

    // close the connection
    conn.Close();
}

That was the delete function from the Category class. Simple, right!

Now let’s look at the Category class:

using System;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;

// A category has many bookmarks
public class Category
{
    // Get all categories.
    public DataTable Get()
    { ....   }


    // Add a new category.
    // Call the Update function and pass 0 for categoryId,
    // the stored procedure will add a new record.
    public void Insert(string title)
    {
        Update(0, title);
    }


    // Update existing category if categroyId > 0,
    // or add a new one if categoryId = 0.
    public void Update(int categoryId, string title)
    {    ...    }

 

    // Delete category by category id.
    public void Delete(int categoryId)
    { ...    }

}

And the Bookmark Class:

using System;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;

// A bookmark has only one category
public class Bookmark
{
    // Get all bookmarks.
   // Pass zero to the stored procedure and it will return all bookmarks.

    public DataTable Get()
    {
        return GetByCategory(0);
    }

    // Get bookmarks by category id.
    public DataTable GetByCategory(int categoryId)
    {    ...    }

 

    // Add a new bookmark.
    // Call the Update function and pass 0 for bookmarkId,
    // the stored procedure will add a new record.
    public void Insert(string title, string url, int categoryId)
    {
        Update(0, title, url, categoryId);
    }


    // Update existing bookmark if bookmarkId > 0,
    // or add a new one if bookmarkId = 0.
    public void Update(int bookmarkId, string title, string url, int categoryId)
    {   ...    }

 

    // Delete bookmark by bookmark id.
    public void Delete(int bookmarkId)
    { ...     }
}

You can view the complete code here:
Category Class
Bookmark Class

In Part V we'll create the web pages and put everything to together.

Countdown - Election Special







Saturday, November 1, 2008

Sarah Palin Pranked

http://www.dailykos.com/storyonly/2008/11/1/151958/557/831/649050

http://www.justiciers.tv/

THANK YOU ATI

I played Brothers in Arms: Hell's Highway last month and had to skip the "Black Friday" chapter because there was a problem with the new ATI cards. Part ATI and part Gearbox.

Gearbox relied on ATI to issue a fix for its drivers, you may think it's ATI's fault (as I did at first) but accroding to a developer at Gearbox, they didn't test the game on the new ATI cards! Only Nvidia!!!

I have Radeon HD 4850 which is SWEEEEEEEEEEET, everything runs sooooothly.

I checked Gearbox's forum today and ATI has released a fix. Installed the fix and it fixed the problem :) I can play the "Black Friday" chapter which has pretty cool effects and scenes.

So, THANK YOU ATI!

Here's the fix: http://support.ati.com/ics/support/default.asp?deptID=894&task=knowledge&questionID=38664