处理从数据库到 GridView 的 NULL [英] Handling NULL from database to GridView

查看:53
本文介绍了处理从数据库到 GridView 的 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 GridView,可以直接从数据库中显示一些值.在数据库中有一个名为 MaxNoPlaces 的列.该列表示 ID 为 ObjectValue 的 TextBox 的最大长度.问题是有时数据库中的值为 NULL,当我运行应用程序绑定失败并且无法加载页面时.如何在数据库中管理 null 并将它们在 maxlenght 中转换为 0 到该文本框?

I have a simple GridView that show some values directy from database. In database there's a column named MaxNoPlaces. That column represents maxlenght of TextBox with id ObjectValue. The problem is sometimes the value in the database is NULL and when I run the app binding fails and page cannot be loaded. How can I manage null in database and convert them to 0 in maxlenght to that textbox ?

提前致谢!

<asp:GridView ID="gvMyObjects" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="GridView" AutoPostBack="False"  AutoGenerateColumns="False" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                    <ContentTemplate>
                        <button class="myBtn" id="Button1" type="button" data-toggle="modal" data-target="#myModal"  runat="server" ><span>Select</span></button>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="ID" >
            <ItemTemplate>
                <asp:Label ID="ObjectID" runat="server"   margin-Left="100px"  Text='<%# Bind("ObjekatID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="ObjectName" runat="server" margin-Left="100px" Text='<%# Bind("ObjectName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Value">
            <ItemTemplate>
                <asp:TextBox ID="ObjectValue" runat="server" margin-Left="100px" MaxLength='<%# Bind("MaxNoPlaces") %>' Text='<%# Bind("ObjectValue") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Value">
            <ItemTemplate>
                <asp:Label ID="Object" runat="server" margin-Left="100px" Text='<%# Bind("Object") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>    
</asp:GridView>

推荐答案

只要你检索数据就可以在 sql 查询中简单地写

You can simply do it in sql query whenever you retrieving data just write

select ISNULL(MaxNoPlaces,0) as MaxNoPlaces FROM table_Name

因此如果数据库具有NULL值,它将返回0.

so it will return 0 if database having NULL value.

这篇关于处理从数据库到 GridView 的 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆