2013-12-23 19 views
0

我把我的asp.net頁面上得到一個錯誤:使用的SqlDataSource的GridView返回「無效列」時,鏈接表是空的

無效列「姓」名。 無效的列名稱'姓氏'。

這些是新表,所以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> 
+0

Girish Vadhel:你是對的。我在頁面上有另一個sqlDataSource拋出錯誤。一旦我的查詢與此類似,一切都奏效了。 – jibboo

回答

1

我不認爲空值導致錯誤在這裏,因爲你說的錯誤指定列名稱不匹配。您可以嘗試在您的查詢中使用別名:

<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>