2010-04-20 28 views
0

我有我的repeater項目模板:異常轉換錯誤(字符串整數)asp.net

<asp:Repeater ID="Linksrepeater" runat="server"> 
<HeaderTemplate><h2>Links</h2><ul> 
</HeaderTemplate> 
<ItemTemplate> 
<li><%#Container.DataItem("Category")%></li> 
</ItemTemplate> 
<FooterTemplate> 
</ul> 
</FooterTemplate> 
</asp:Repeater> 

迷上了:

s = "sql" 
     x = New SqlCommand(s, c) 
     x.Parameters.Add("@contentid", SqlDbType.Int) 
     x.Parameters("@contentid").Value = contentid 
     c.Open() 
     r = x.ExecuteReader 
     If r.HasRows Then 
      Linksrepeater.DataSource = r 
      Linksrepeater.DataBind() 
     End If 

     c.Close() 
     r.Close() 

當我運行的代碼我得到:

用戶代碼未處理無效的投射異常(從 字符串「category」轉換爲類型「Integer」無效。)

我不知道如何/爲什麼它試圖將Category轉換爲整數,就像在db中它是一個字符串一樣。

你能告訴我如何避免這個錯誤?謝謝。

編輯:如果我嘗試<%#Container.DataItem("URL")%>

+0

要在這裏需要一個完整的代碼示例,包括SQL字符串。 – 2010-04-20 06:54:03

+0

是的,你認爲的工作可能是什麼不工作,這就是爲什麼你在這裏,對嗎? ;-)發佈中繼器的sql和標記。 – 2010-04-20 06:55:55

回答

0

你確定這個片段是正確的發生

同樣的錯誤?

<%# Container.DataItem("Category") %> 

試試這個:

<%# Eval("Category") %> 

或(改變爲MyObject你的類名)

<%# (Container.DataItem as MyObject).Category %> 
相關問題