I'm a little stumped as to how to fix an error here. I have a gridview with a series of numbers (samples of the way they appear in the database are below) that I want to add commas to. I'm attempting to get this done in the ASP code with a literal (code below). The table associated with the stuff below lets call foo and the column name is called 'count'. Without the TemplateField and the literal (noted below) the gridview works fine. However, when I add the TemplateField below, and I get an 'Object cannot be cast from DBNull to other types.' from the browser. All I'm trying to do is to get the gridview output to show commas in the ASP code and not the database.
关于如何修复错误,我有点难过。我有一个gridview,其中包含一系列数字(它们在数据库中的显示方式示例如下),我想添加逗号。我试图在ASP代码中使用文字(下面的代码)完成此操作。与下面的内容相关联的表允许调用foo,列名称称为“count”。没有TemplateField和文字(如下所示),gridview工作正常。但是,当我在下面添加TemplateField时,我得到一个'对象无法从DBNull转换为其他类型。'来自浏览器。我所要做的就是让gridview输出在ASP代码而不是数据库中显示逗号。
What I've tried:
我尝试过的:
First I thought there might be some odd characters, trailing spaces or things like that so I ran the following command:
首先我想可能有一些奇怪的字符,尾随空格或类似的东西,所以我运行以下命令:
select * from foo where count not like '%[^0-9]%'
Only it returned every single entry in the table. I manually checked some of the results and there aren't any trailing spaces or odd characters in the columns I want to add commas to.
只返回表中的每个条目。我手动检查了一些结果,并且我想要添加逗号的列中没有任何尾随空格或奇数字符。
I'm pretty stumped as what to try next as I've been able to get this to work on my test server but not in production.
我很难接受下一步的尝试,因为我已经能够在我的测试服务器上工作但不能在生产中工作。
Column Sample
专栏样本
Count
2345342
4356546
3245234534
67465745675
43523452345
546234523452345
32452345
4352345234
4352345
345234
2345342 4356546 3245234534 67465745675 43523452345 546234523452345 32452345 4352345234 4352345 345234
These are entered into the database as NVARCHAR(MAX) (nulls allowed) as we import data from a wide variety of sources.
当我们从各种来源导入数据时,它们作为NVARCHAR(MAX)(允许空值)输入数据库。
Code
码
<asp:TemplateField HeaderText="Count" SortExpression="count">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("count") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server" Text='<%# string.Format("{0:N0}", Convert.ToDecimal(Eval("count"))) %>'>
</asp:Literal>
</ItemTemplate>
<HeaderStyle BackColor="#6699FF" />
</asp:TemplateField>
Error
错误
Object cannot be cast from DBNull to other types.
无法将对象从DBNull强制转换为其他类型。
2 个解决方案
#1
1
I'm guessing somewhere you have a null and if you try to convert it to decimal in your template you would get that error. You'll need to put some code to disallow that.
我猜你某处有一个null,如果你试图在模板中将它转换为十进制,你会得到那个错误。你需要放一些代码来禁止它。
string.Format("{0:N0}", Convert.ToDecimal(Eval("count")==DBNull.Value ? "0" : Eval("count")))
#2
1
Have you considered in your query wrapping the column that may contain nulls with an IsNull. Take the problem away at the database
您是否在查询中考虑了包含带有IsNull的空值的列。在数据库中解决问题
select *,isnull([count],0) from foo where count not like '%[^0-9]%'
从foo中选择*,isnull([count],0),其中count不像'%[^ 0-9]%'
#1
1
I'm guessing somewhere you have a null and if you try to convert it to decimal in your template you would get that error. You'll need to put some code to disallow that.
我猜你某处有一个null,如果你试图在模板中将它转换为十进制,你会得到那个错误。你需要放一些代码来禁止它。
string.Format("{0:N0}", Convert.ToDecimal(Eval("count")==DBNull.Value ? "0" : Eval("count")))
#2
1
Have you considered in your query wrapping the column that may contain nulls with an IsNull. Take the problem away at the database
您是否在查询中考虑了包含带有IsNull的空值的列。在数据库中解决问题
select *,isnull([count],0) from foo where count not like '%[^0-9]%'
从foo中选择*,isnull([count],0),其中count不像'%[^ 0-9]%'