Click here to Skip to main content
15,939,243 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I have a database table called [PortfolioPerformance]
SQL
Portfolio, int, not null
AnnualReturn, decimal(18,2), not null
Closed, bit, not null

and a SqlDataSource
ASP.NET
<asp:SqlDataSource ID="PerformanceDataSource" runat="server"
SelectCommand="SELECT [Portfolio], [AnnualReturn], [Closed] FROM [PortfolioPerformance]"/>

and a ListView
ASP.NET
<asp:ListView ID="PerformanceListView" runat="server" DataSourceID="PerformanceDataSource">
...
<ItemTemplate>
  <tr>
    <td>
      <asp:CheckBox ID="ClosedCheckBox" runat="server" Checked='<%# Eval("Closed") %>' Enabled="false" />
    </td>
  </tr>
</ItemTemplate>

And at runtime I get a HttpException "DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Closed'". If I changed the CheckBox to Checked='false', everything is fine, except the display is wrong.

I used a ListView so that users could edit the entry, and editing works fine (that is, the SqlDataSource has no problems with the insert, update, or delete statements, and using Bind("Closed") in the ListView templates doesn't throw exceptions). I've run the select statement in SQL Management Studio and it returns the correct results. Can anyone help?
Posted

You need to conver the bit to boolean please refer the following links and convert the value to boolean and then assign the value to checked property..


http://stackoverflow.com/questions/1561382/asp-net-binding-integer-to-checkboxs-checked-field[^]

http://forums.asp.net/t/917702.aspx/1[^]


http://stackoverflow.com/questions/2767352/c-sharp-convert-bit-to-boolean[^]
 
Share this answer
 
By Using Ternary Operator or Case or Condition you can display the result according to your condition.
 
Share this answer
 
A Case condition doesn't work. When I change the select statement to read
SQL
SELECT [Portfolio]
      ,[AnnualReturn]
      ,case
         when [Closed] = 0 then 'false'
         else 'true'
       end as 'Closed'
FROM [PortfolioPerformance]

I still get the exception.

What I don't understand is why the view seems to think there isn't a field for Closed - "'System.Data.DataRowView' does not contain a property with the name 'Closed'". I would think a conversion error would produce a different exception and error message.

Likewise, a bit conversion still needs to have the field present, so if the DataRowView can't see it, it doesn't matter if the bit is converted or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900