The presence of bit binding to the flag

I have a flag associated with the database in a field in my database, and that field is a bit in the database.

This is my checkbox:

<asp:CheckBox ID="chkDownloaded" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem, "Downloaded")%>' /> 

When I run the code, I get the following error:

 Specified cast is not valid. 

Any idea how to fix this?

Thanks!

+4
source share
5 answers

Option number 1

Your Database query should look like this.

 Select Cast(IsNull(Downloaded, 0) as bit) as Downloaded From TableName 

Option number 2

In Business Logic Layer Convert , Downloaded to false in the case of Null .

Option number 3

 protected void GridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { CheckBox c = e.Row.FindControl("chkDownloaded"); (((YourClassName)e.Row.DataItem).YourPropertyName) == null ? false : ((YourClassName)e.Row.DataItem).YourPropertyName; } 
+2
source

Try

 DataBinder.Eval(Container.DataItem, "Downloaded") ?? false 
+3
source

you will need to convert it to boolean and bind

This way

 <asp:CheckBox ID="chkDownloaded" runat="server" Checked='<%#Eval("Downloaded").ToString() == "1")%>' /> 
+1
source

Try:

 <%#DataBinder.Eval(Container.DataItem, "Downloaded")!=0%> 
0
source

Not an ASP.NET expert, but if the column is nullified, then

 Checked='<%#(Eval("Downloaded") ?? 0).ToString() == "1")%>' /> 
-1
source

All Articles