GridView:在服务器上运行时出现数据库错误,在localhost上运行正常

时间:2012-09-19 13:56:44

标签: c# asp.net sql

我正在开展一个项目,我必须在gridview中返回以下列,并且可以通过0-7过滤器缩小搜索范围。

  • 的AppID
  • CLIENTNAME
  • ProjectNo。
  • ProjectSubNo。
  • ProjectTitle
  • 的AppType
  • AppSubType
  • 节点名
  • 用户名

我可以在我的本地计算机上执行此操作,但是当我在服务器上运行它时,它会返回错误,指出它无法在数据库源中找到ProjectTitle字段。其余的代码很好,因为从GridView中删除ProjectTitle列时没有显示错误。以下是我的代码。

ASP

<asp:GridView ID="GridView2" runat="server"
            CellPadding="4" 
            ForeColor="#333333" 
            GridLines="None" 
            onselectedindexchanged="GridView1_SelectedIndexChanged" 
        AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="AppID" HeaderText="AppID" InsertVisible="False" 
                ReadOnly="True" SortExpression="AppID" />
            <asp:BoundField DataField="ClientName" HeaderText="ClientName" 
                SortExpression="ClientName" />
            <asp:BoundField DataField="ProjectNumber" HeaderText="ProjectNo." 
                SortExpression="ProjectNumber" />
            <asp:BoundField DataField="ProjectSubNumber" HeaderText="SubNo." 
                SortExpression="ProjectSubNumber" />
            <asp:BoundField DataField="ProjectTitle" HeaderText="ProjectTitle" 
                SortExpression="ProjectTitle" />
            <asp:BoundField DataField="AppType" HeaderText="AppType" 
                SortExpression="AppType" />
            <asp:BoundField DataField="AppSubType" HeaderText="AppSubType" 
                SortExpression="AppSubType" />
            <asp:BoundField DataField="NodeName" HeaderText="NodeName" 
                SortExpression="NodeName" />
            <asp:BoundField DataField="Username" HeaderText="Username" 
                SortExpression="Username" />
            <asp:CommandField ShowSelectButton="True" ButtonType="Image" 
                SelectImageUrl="~/buttons/png/Select.png" />
        </Columns>
        <AlternatingRowStyle BackColor="White" />
        <FooterStyle BackColor="#D11011" 
                Font-Bold="True" 
                ForeColor="White" />
        <HeaderStyle BackColor="#D11011" 
                Font-Bold="True" 
                ForeColor="White" />
        <PagerStyle BackColor="#FDEBEB" 
                ForeColor="#333333" 
                HorizontalAlign="Center" />
        <RowStyle BackColor="#FDEBEB" 
                ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FDCCCC" 
                Font-Bold="True" 
                ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDEBEB" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FDEBEB" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NeoDyneNewTMS %>" SelectCommand="SELECT [AppID],[ClientName], [ProjectNumber], [ProjectSubNumber], [ProjectTitle], [AppType], [AppSubType], [NodeName], [Username] FROM [PMS_AppRegister],[TMS_Clients],[TMS_ProjectRegister],[PMS_AppTypes],[PMS_AppSubTypes] WHERE [TMS_Clients].[ClientID] = [TMS_ProjectRegister].[ClientID] AND [PMS_AppRegister].[ProjectID] = [TMS_ProjectRegister].[ProjectID] AND [PMS_AppRegister].[AppTypeID] = [PMS_AppTypes].[AppTypeID] AND [PMS_AppRegister].[AppSubTypeID] = [PMS_AppSubTypes].[AppSubTypeID] ORDER BY [AppID] DESC">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="PMS_sch_ds_SELECTAll" 
            runat="server" 
            ConnectionString="<%$ ConnectionStrings:NeoDyneNewTMS %>" 
            SelectCommand="">
        <SelectParameters>
            <asp:ControlParameter ControlID="PMS_sch_ddl_ClientName" 
                    Name="ClientID" 
                    PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="PMS_sch_ddl_ProjectNumber" 
                    PropertyName="SelectedValue" 
                    Name="ProjectNumber" 
                    DefaultValue=null/>
            <asp:ControlParameter ControlID="PMS_sch_ddl_ProjectSubNumber" 
                    Name="ProjectID" 
                    PropertyName="SelectedValue" 
                    DefaultValue=null />
            <asp:ControlParameter ControlID="PMS_sch_ddl_ApplicationType" 
                    Name="AppTypeID" 
                    PropertyName="SelectedValue" 
                    DefaultValue=null />
            <asp:ControlParameter ControlID="PMS_sch_ddl_ApplicationSubType" 
                    Name="AppSubTypeID" 
                    PropertyName="SelectedValue" 
                    DefaultValue=null />
            <asp:ControlParameter ControlID="PMS_sch_tb_NodeName" 
                    Name="NodeName" 
                    PropertyName="Text" 
                    DefaultValue=null />
        </SelectParameters>
    </asp:SqlDataSource>

C#

protected void btnsearch_Click(object sender, ImageClickEventArgs e)
        {
            GridView2.DataSourceID = "PMS_sch_ds_SELECTAll";
            #region Select Command for the Grid View
            //initializes the string for SelectCommand
            string SelectCommand = "";
            if (PMS_sch_ddl_ClientName.SelectedValue != "")
            {
                if (PMS_sch_ddl_ProjectNumber.SelectedValue != "")
                {
                    if (PMS_sch_ddl_ProjectSubNumber.SelectedValue != "")
                    {
                        if (PMS_sch_ddl_ApplicationType.SelectedValue != "")
                        {
                            if (PMS_sch_ddl_ApplicationSubType.SelectedValue != "")
                            {
                                #region Client Name, Project Number, Project Sub Number, Application Type, Application Sub Type, Node Name
                                if (PMS_sch_tb_NodeName.Text != "")
                                {
                                    SelectCommand = "SQL";
                                }
                                #endregion
                                #region Client Name, Project Number, Project Sub Number, Application Type, Application Sub Type
                                else
                                {
                                    SelectCommand = "SQL";
                                }
                                #endregion
                            }
                            else
                            {
                                #region Client Name, Project Number, Project Sub Number, Application Type, Node Name
                                if (PMS_sch_tb_NodeName.Text != "")
                                {
                                    SelectCommand = "SQL";
                                }
                                #endregion
                                #region Client Name, Project Number, Project Sub Number, Application Type
                                else
                                {
                                    SelectCommand = "SQL";
                                }
                                #endregion
                            }
                        }
                        else
                        {
                            #region Client Name, Project Number, Project Sub Number, Node Name
                            if (PMS_sch_tb_NodeName.Text != "")
                            {
                                SelectCommand = "SQL";
                            }
                            #endregion
                            #region Client Name, Project Number, Project Sub Number
                            else
                            {
                                SelectCommand = "SQL";
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        if (PMS_sch_ddl_ApplicationType.SelectedValue != "")
                        {
                            if (PMS_sch_ddl_ApplicationSubType.SelectedValue != "")
                            {
                                #region Client Name, Project Number, Application Type, Application Sub Type, Node Name
                                if (PMS_sch_tb_NodeName.Text != "")
                                {
                                    SelectCommand = "SQL";
                                }
                                #endregion
                                #region Client Name, Project Number, Application Type, Application Sub Type
                                else
                                {
                                    SelectCommand = "SQL";
                                }
                                #endregion
                            }
                            else
                            {
                                #region Client Name, Project Number, Application Type, Node Name
                                if (PMS_sch_tb_NodeName.Text != "")
                                {
                                    SelectCommand = "SQL";
                                }
                                #endregion
                                #region Client Name, Project Number, Application Type
                                else
                                {
                                    SelectCommand = "SQL";
                                }
                                #endregion
                            }
                        }
                        else
                        {
                            #region Client Name, Project Number, Node Name
                            if (PMS_sch_tb_NodeName.Text != "")
                            {
                                SelectCommand = "SQL";
                            }
                            #endregion
                            #region Client Name, Project Number
                            else
                            {
                                SelectCommand = "SQL";
                            }
                            #endregion
                        }
                    }
                }
                else
                {
                    if (PMS_sch_ddl_ApplicationType.SelectedValue != "")
                    {
                        if (PMS_sch_ddl_ApplicationSubType.SelectedValue != "")
                        {
                            #region Client Name, Application Type, Application Sub Type, Node Name
                            if (PMS_sch_tb_NodeName.Text != "")
                            {
                                SelectCommand = "SQL";
                            }
                            #endregion
                            #region Client Name, Application Type, Application Sub Type
                            else
                            {
                                SelectCommand = "SQL";
                            }
                            #endregion
                        }
                        else
                        {
                            #region Client Name, Application Type, Node Name
                            if (PMS_sch_tb_NodeName.Text != "")
                            {
                                SelectCommand = "SQL";
                            }
                            #endregion
                            #region Client Name, Application Type
                            else
                            {
                                SelectCommand = "SQL";
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        #region Client Name, Node Name
                        if (PMS_sch_tb_NodeName.Text != "")
                        {
                            SelectCommand = "SQL";
                        }
                        #endregion
                        #region Client Name
                        else
                        {
                            SelectCommand = @"SELECT    DISTINCT [AppID], [ClientName], [ProjectNumber], [ProjectSubNumber], [ProjectTitle], [AppType], [AppSubType], [NodeName], [PMS_AppRegister].[Username]
                            FROM    [TMS_Clients], [TMS_ProjectRegister], [PMS_AppTypes], [PMS_AppSubTypes],
                                    [PMS_AppRegister], [TMS_EmployeeRegister]
                            WHERE   [TMS_Clients].[ClientID]                    = @ClientID                         AND
                                    [TMS_Clients].[ClientID]                    = [TMS_ProjectRegister].[ClientID]  AND
                                    ([ProjectNumber] IN (SELECT [ProjectNumber]
                                                        FROM    [TMS_ProjectRegister]
                                                        WHERE   [ProjectID] IN  (SELECT [ProjectID]
                                                                                FROM    [PMS_AppRegister])      AND
                                                                [ClientID] = @ClientID))                            AND
                                    ([ProjectSubNumber] IN  (SELECT [ProjectSubNumber]
                                                            FROM    [TMS_ProjectRegister]
                                                            WHERE   [ProjectID] IN  (SELECT [ProjectID]
                                                                                    FROM    [PMS_AppRegister])  AND
                                                                    [ClientID] = @ClientID))                        AND
                                    [PMS_AppRegister].[ProjectID]               = [TMS_ProjectRegister].[ProjectID] AND
                                    [PMS_AppRegister].[AppTypeID]               = [PMS_AppTypes].[AppTypeID]        AND
                                    [PMS_AppSubtypes].[AppTypeID]               = [PMS_AppRegister].[AppTypeID]     AND
                                    [PMS_AppSubTypes].[AppSubTypeID]            = [PMS_AppRegister].[AppSubTypeID]";
                        }
                        #endregion
                    }
                }
            }
            else if (PMS_sch_ddl_ClientName.SelectedValue == "")
            {
                GridView2.DataSourceID = "SqlDataSource1";
            }
            //Lets the datasource's Select Command equal to the correct string
            PMS_sch_ds_SELECTAll.SelectCommand = SelectCommand;
            #endregion
            //GridView1.DataBind();
        }

有关导致此问题的任何建议吗?

1 个答案:

答案 0 :(得分:1)

您确定远程数据库包含字段ProjectTitle吗?

如果更改localhost连接字符串localhost以便它使用远程数据库(但仍使用localhost项目),您仍然会收到错误吗? (这将确定问题是在代码中还是在数据库中。)