You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C#/ASP.NET/SQL Server开发法务DBMS时GridView加载空值异常求助

解决ASP.NET GridView绑定报错:Value cannot be null. Parameter name: propName

问题场景

使用C#、ASP.NET和SQL Server开发企业法务数据库管理系统,Cases.aspx.cs中的loadrecord方法通过存储过程AdminViewCases加载数据到GridView,页面访问时抛出异常:

unhandled exception occurred during the execution of the current web request. Value cannot be null. Parameter name: propName

项目中其他类似页面(如查看律师、对手页面)使用相同方法可正常运行。

关键代码

Cases.aspx.cs 加载数据方法

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Legal1
{
    public partial class Cases : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadrecord();
            }
        }

        protected void loadrecord()
        {
            String connstr = WebConfigurationManager.ConnectionStrings["Legal"].ToString();
            SqlConnection conn = new SqlConnection(connstr);
            SqlCommand comm = new SqlCommand("AdminViewCases", conn);
            comm.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapter = new SqlDataAdapter(comm);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            Gv.DataSource = dt;
            Gv.DataBind();
        }
    }

Cases.aspx GridView配置

<asp:GridView ID="Gv" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#FF9900" GridLines="None" Height="269px" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:BoundField DataField="Serial_Number" HeaderText="Serial_Number" />
        <asp:BoundField DataField="Title" HeaderText="Title" />
        <asp:BoundField DataField="Judge" HeaderText="Judge" />
        <asp:BoundField DataField="Prosecutor" HeaderText="Prosecutor" />
        <asp:BoundField DataField="Oponnent" HeaderText="Oponnent" />
        <asp:BoundField DataField="Type" HeaderText="Type" />
        <asp:BoundField DataField="status" HeaderText="Status" />
        <asp:BoundField DataField="Last session" HeaderText="Last Session" />
        <asp:BoundField DataField="Next session" HeaderText="Next session" />
        <asp:BoundField DataField="startDate." HeaderText="Start Date" />
        <asp:BoundField DataField="Lawyer" HeaderText="Lawyer" />
        <asp:BoundField DataField="Lawyer_Id" HeaderText="Lawyer_Id" />
    </Columns>
    <EditRowStyle BackColor="#FF9900" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#FF9900" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#FF9900" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

SQL Server存储过程

CREATE Proc AdminViewCases
as
begin
Select Serial_Number,Title,Judge,Prosecutor,Oponnent,type,status,[Last session],
[Next session],startDate,Lawyer,Lawyer_Id
from Cases
end

问题分析

异常提示propName为空,说明GridView绑定数据时,某个BoundField的DataField指定的字段在查询结果中不存在,导致系统无法找到对应的属性名。

对比存储过程返回的字段和GridView的DataField配置:

  • 存储过程返回字段为startDate,但GridView中对应的DataField写成了startDate.(多了一个末尾的点),字段名不匹配,导致绑定失败。

解决方案

修改GridView中对应字段的DataField,去掉多余的点:

<!-- 原错误代码 -->
<asp:BoundField DataField="startDate." HeaderText="Start Date" />

<!-- 修改后 -->
<asp:BoundField DataField="startDate" HeaderText="Start Date" />

另外可做以下规范调整(非强制但建议):

  • 存储过程中返回的type是小写,GridView中DataField写的是Type,可统一为相同大小写(如修改存储过程SELECT语句为Type),避免潜在的匹配问题。
  • 字段Oponnent疑似拼写错误(正确应为Opponent),如果数据库中字段名确实是Oponnent则无需修改,否则需同步调整GridView和存储过程的字段名。

验证修改

修改后重新运行项目,访问Cases.aspx页面,即可正常加载GridView数据。

内容的提问来源于stack exchange,提问作者Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 05:25:53