开发者

webmatrix pass ID to display code

I am currently going nuts making a page to display the contents when an ID is passsed to it. The code i have can be found below

 @{  
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Home";
var db = Database.Open("StarterSite");
 var PageID  = UrlData[0]; 
 if (PageID.IsEmpty()) { 
     PageID = "1";
 } 
var pages = db.QuerySingle("SELECT * FROM PAGES WHERE Id = @0",PageID);

}

        <div class="productInfo">
            <h3>@pages.Body</h3>
            </div>

Basically what i am trying to get it to do is when you enter the url default/1 that the body of the page w开发者_StackOverflow中文版hich has an id of 1 will appear and that if no querystring is given it will default to ID 1

When i run this i get an error with the line

@pages.Body

stating Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

the URL passed is http://localhost:20756/Default/1 and there is a page with an ID of 1 in the system


the exception in other words:

your runtime said:

"you gave me a query that did not return anything, but want me to access some data on that result ... errm ... that's not going to happen"

if this code runs in your default.cshtml, try removing "Default/" from your url ...

note that PageID.IsEmpty() is not evaluating to true since UrlData[0] will probably contain "Default"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜