3

I want to create a computed column in SQL SERVER and set the formula to this

([Category] +  '.aspx?ID=' + [Post_ID])

Not working though......what am i missing?

Category and Post_ID are current columns in the table

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Etienne
  • 7,141
  • 42
  • 108
  • 160

3 Answers3

3

Do you do

SELECT [Category] +  '.aspx?ID=' + [Post_ID]
FROM table

?

Try

SELECT CAST([Category] AS NVARCHAR(MAX)) + '.aspx?ID=' CAST([Post_ID] AS NVARCHAR(MAX))
FROM table

or specify max size depending on your columns' data type.


Above answer is bad. DON'T FORMAT ON DATA LAYER. FORMAT ON PRESENTATION LAYER, i.e. in mark-up. E.g.:

<asp:HyperLinkField
    HeaderText="LinkHeader"
    DataNavigateUrlFormatString="{0}.aspx?ID={1}" 
    DataNavigateUrlFields="Category,Post_ID"
    DataTextField="LinkName" />

(to work properly this requires also a field LinkName to exists in the resulting selection)

or

<asp:Hyperlink runat= "server"
    Text='<%# DataBinder.Eval(Container.DataItem, "LinkName").ToString() %>' 
    NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Category").ToString() + ".aspx?ID=" + DataBinder.Eval(Container.DataItem, "Post_ID").ToString() />   
abatishchev
  • 98,240
  • 88
  • 296
  • 433
1

I guess you're probably missing a cast. I agree that this seems an unlikely candidate for a computed column though.

create table #t
(
Category varchar(50),
[Post_ID] int
)

alter table #t 
add comp as ([Category] +  '.aspx?ID=' + cast([Post_ID] as varchar(10)))
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
0

This actually sorted it out for me at the end.....

NavigateUrl='<%# String.Format("{0}.aspx?ID={1}", DataBinder.Eval(Container.DataItem, "Category"), DataBinder.Eval(Container.DataItem, "Post_ID")) %>'
Etienne
  • 7,141
  • 42
  • 108
  • 160