开发者

Create computed column with string in formula

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


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() />   


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)))


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")) %>'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜