views:

804

answers:

5

I have a table that contains three columns.

"UserId" type-nvarchar

"PostAuthorId" type-nvarchar

"Post" type-text

This table will contain "wall" posts like in facebook for each user's page. I am going to use a gridview on each user's page to display the posts. The issue is I want to display them with the latest(most current) post being first and the earliest post being last.

I have never used autoincrement before and I am not sure if that is the answer. If it is, I do not know how to use it. I thought about adding a date posted column and then ordering by date.

If I end up using the date column, I could also display the date on the post. Is there a way to convert the date to a readable format?

What is the best way of implementing this type of ordering?

+1  A: 

A datetime column would definitely work for something like this. Assuming you are using MS-SQL, you can also attach a default value to the column using a built-in function like GETDATE(). That way, you only have to input the data that matters and the database will take care of adding the datetime column.

Ryan
How do you add the GETDATE() function to that column? I am assuming you mean when you add a record, it automatically inserts the current date without any work on my side? How do I do that? I am using MS-SQL Express.
In the Design view for your table, highlight the datetime column. Then, down in the column properties there should be an option for "Default Value or Binding". Set that to GETDATE().
Ryan
And yes, it will automatically inserts the current date and time into that column when you insert a new row.
Ryan
Sweetcoder, you'll do something similar to create an Identity field. Create an Int field (I always call mine UID) and, with it selected, go down into the properties area. Click the "+" next to "Identity" and then, in the subfields, toggle "Identity" to True. Don't worry about other settings.
Mark Brittingham
A: 

For converting a datetime to a readable format try:

DateTime postDate;
string value = postDate.ToShortDateString();
mdresser
A: 

I would suggest the DateTime field rather than the autoincrement simply because it will not only serve as an effective Sort field, it also preserves information that you may well want to display. If you want the most recent first you'll sort using the Date and a "DESC" modifier:

Select ... Order By [Date] DESC;

When you retrieve the data, you can retrieve it as a DateTime and modify it using C#. You can use "ToShortDateString()" as suggested by mdresser if you just wish to show the date or ToString("...") if you wish to show the time as well. You can also use SQL to convert it into a string before retrieving it:

convert(Varchar(10), @mydatetime, 101)

If you look in MSDN you'll see the various conversion codes (101 is the code used above) that can be used to translate the date in various ways.

UPDATE: You may want to use an autoincrementing field for your application for reasons other than your expressed need to sort wall entries. They are easy to use - just mark the field as an Identity if using SQL Server (other DBs are similar). As far as using them in your program, just think of the field as an Int field that you never have to set.

Now, why would you use a auto-incrementing field? Perhaps the most straightforward reason is so that they give you have an easy way to identify each record. For example, if you permit people to alter or delete their wall entries, the auto-incrementing field is ideal as it gives you a way to easily look up each record (each record will be assigned its own, unique value). You might put an "x" next to the record like StackOverflow does and make it a call back with the UID (auto-increment) value. Note that you should set up your primary key on the UID field if you'll be doing this.

Now, if you find them useful for this reason then you could also sort by the UID. I would still store the date so that you can provide Date and Time feedback as to when an entry was made on the wall but this would no longer be your indexed or sorted field.

Mark Brittingham
+2  A: 

If you use AutoIcrement the first record will start with 1 and each record will increment from there. (default setting)

If you want to sort them by newest first do an ORDER BY ID DESC I would suggest making a column called wallPostID then setting that to AutoIncrement and also your Primary Key

Date Formating: If you are displaying this data in a gridView

  1. Go to Edit Columns on your grid view
  2. CLick on the Date field under "Selected Fields" on the bottom left
  3. Under "BoundField properties" on the right Go to Data -> DataFormatString
  4. {0:d} will display as 1/1/2010

This site has more info in string formatting

http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

Gthompson83
+1 - Nice answer, good examples and instructions.
Mark Brittingham
A: 

You should always use an ID field that auto increments. Can also be used as your PK

-1 I'm sorry, but I don't think that this is helpful advice. It simply isn't true that you "always" use an autoincrement field.
Mark Brittingham