当链接表为空时,使用SQLDataSource的Gridview返回“无效列”

时间:2013-12-23 16:31:03

标签: asp.net gridview sqldatasource

我的asp.net页面上一直出现错误:

  

列名称'FirstName'无效。   无效的列名称“LastName”。

这些是新表,因此aspnet_users在用户或联系人中没有任何匹配的记录。因此,我假设它由于名字和姓氏中的NULL值而爆炸。有没有办法告诉gridview只返回一个空字符串?

<asp:GridView ID="grdStaff" runat="server" AllowSorting="True" AutoGenerateColumns="False" 
DataSourceID="SqlStaff">
   <Columns>
     <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
     <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
     <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName"  />
     <asp:CheckBoxField DataField="Active" HeaderText="Active" SortExpression="Active" />
     <asp:BoundField DataField="LastActivityDate" HeaderText="LastActivityDate" SortExpression="LastActivityDate" />
   </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlStaff" runat="server" 
ConnectionString="<%$ ConnectionStrings:VTConnectionString %>" 
SelectCommand="SELECT au.UserId, au.UserName, au.LastActivityDate, Contact.FirstName, Contact.LastName, u.Active FROM Contact RIGHT OUTER JOIN Users AS u ON Contact.ContactID = u.ContactID RIGHT OUTER JOIN aspnet_Users AS au ON u.UserId = au.UserId"></asp:SqlDataSource>

1 个答案:

答案 0 :(得分:1)

我不认为null值会导致此处出现错误,因为您声明的错误指定列名称不匹配。您可以在查询中尝试使用别名,例如:

<asp:SqlDataSource ID="SqlStaff" runat="server" 
ConnectionString="<%$ ConnectionStrings:VTConnectionString %>" 
SelectCommand="SELECT au.UserId, au.UserName, au.LastActivityDate, Contact.FirstName as FirstName, Contact.LastName as LastName, u.Active FROM Contact RIGHT OUTER JOIN Users AS u ON Contact.ContactID = u.ContactID RIGHT OUTER JOIN aspnet_Users AS au ON u.UserId = au.UserId"></asp:SqlDataSource>