I’m trying to get blogposts by dates to build up an archive, more specifically I want the data to be presented by month of the year, e.g. July 2012, August 2012 and so on. And oh, consider me a newbie.
The data is retrieved by a ListView that renders the Date in a Label, the label text (e.g. Juni, 2012) is then forwarded to a new SP in code-behind that I’m currently struggling with. I am trying to check the parameter with my date field with the LIKE clause, but somehow I get both June och July when i try the SP with param “juli”. It gets a bit confusing with the data types, i don’t really know what to use in this situation.
SP:
I’ve left the length out of the param data type, it give me any data if the length is defined, should be 10 though. I am also trying to convert the datetime from the table to a somewhat similar string to @Date I’m kind of lost in this and not so familiar with the LIKE clause, so please consider me a newbie at this.
@Date varchar
AS
BEGIN
SELECT A.ArticleID, A.Name, A.Header, A.Article, A.Date,
C.Category, C.CategoryID,
M.FirstName + ' ' + M.LastName as 'Author', A.ArticleLevel
FROM Article as A
LEFT JOIN Category as C on A.CategoryID = C.CategoryID
LEFT JOIN Member as M ON A.MemberID = M.MemberID
WHERE CONVERT(VARCHAR(4), a.Date, 100) + CONVERT(VARCHAR(4), a.Date, 120) LIKE @Date + '%'
ORDER BY A.Date, A.ArticleLevel Asc
END
exec [usp_GetArticlesForArticleDates] 'juli'
C#:
The “ArchiveArticlesListView” is the nested ListView that renders the articles for the specific dates.
protected void ArchiveListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var articleId = (int)ArchiveListView.DataKeys[e.Item.DataItemIndex].Value;
//var dateParam = (int)ArchiveListView.DataKeys[e.Item.DataItemIndex].Value; // Tried to use the DataKey for Date and convert it to char, but that only left me with values like "309 j"
var DateLabel = e.Item.FindControl("DateLabel") as Label;
var date = DateLabel.Text;
var articlesLV = e.Item.FindControl("ArchiveArticlesListView") as ListView;
articlesLV.DataSource = Service.GetArticleArchive(date);
articlesLV.DataBind();
}
}
try the following if you want to get data between two dates:
I think this will work for you.