将存储过程结果集绑定到GridView

时间:2014-05-21 18:21:25

标签: asp.net gridview stored-procedures binding

我试图将存储过程的结果集绑定到ASP.NET中的GridView。但是,结果集似乎不具有约束力。我已经完成了我的代码,一切看起来都运行得很好:我的连接不会丢失,我的参数传递得很好。我甚至没有收到错误消息!我只是没有得到返回的结果集。当我添加一个断点并检查调试器中的数据集对象时,它是空的。

我在SSMS中运行了存储过程,并且已经验证我应该返回结果集。以下是我的存储过程和我的应用程序中的一些代码。

请告诉我我做错了什么。感谢。

存储过程:

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'xxxx.spGetFromHours_ServiceUsers'))
DROP PROCEDURE xxxx.spGetFromHours_ServiceUsers

GO

CREATE PROCEDURE xxxx.spGetFromHours_ServiceUsers
(
    @UserTxtFName VARCHAR(20),
    @UserTxtLName VARCHAR(20),
    @UserTxtFrDate VARCHAR(20),
    @UserTxtToDate VARCHAR(20),
    @OrderBy VARCHAR(500)
)

AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

If(@OrderBy = 'LFT')
BEGIN
    SELECT Project_FK, TaskID_FK, UserName_FK, CAST(TaskDate AS DATETIME2), Duration, Description, Bill, Active, FirstName, MiddleName, LastName
    FROM xxxx.xxxx.vw_Hours_ServiceUsers
    WHERE FirstName LIKE @UserTxtFName AND LastName LIKE @UserTxtLName AND TaskDate BETWEEN @UserTxtFrDate AND @UserTxtToDate
    ORDER BY LastName, FirstName, TaskDate
END

If(@OrderBy = 'LTF')
BEGIN
    SELECT Project_FK, TaskID_FK, UserName_FK, CAST(TaskDate AS DATETIME2), Duration, Description, Bill, Active, FirstName, MiddleName, LastName
    FROM xxxx.xxxx.vw_Hours_ServiceUsers
    WHERE FirstName LIKE @UserTxtFName AND LastName LIKE @UserTxtLName AND TaskDate BETWEEN @UserTxtFrDate AND @UserTxtToDate
    ORDER BY LastName, TaskDate, FirstName
END

If(@OrderBy = 'FLT')
BEGIN
    SELECT Project_FK, TaskID_FK, UserName_FK, CAST(TaskDate AS DATETIME2), Duration, Description, Bill, Active, FirstName, MiddleName, LastName
    FROM xxxx.xxxx.vw_Hours_ServiceUsers
    WHERE FirstName LIKE @UserTxtFName AND LastName LIKE @UserTxtLName AND TaskDate BETWEEN @UserTxtFrDate AND @UserTxtToDate
    ORDER BY FirstName, LastName, TaskDate
END

If(@OrderBy = 'FTL')
BEGIN
    SELECT Project_FK, TaskID_FK, UserName_FK, CAST(TaskDate AS DATETIME2), Duration, Description, Bill, Active, FirstName, MiddleName, LastName
    FROM xxxx.xxxx.vw_Hours_ServiceUsers
    WHERE FirstName LIKE @UserTxtFName AND LastName LIKE @UserTxtLName AND TaskDate BETWEEN @UserTxtFrDate AND @UserTxtToDate
    ORDER BY FirstName, TaskDate, LastName
END

If(@OrderBy = 'TFL')
BEGIN
    SELECT Project_FK, TaskID_FK, UserName_FK, CAST(TaskDate AS DATETIME2), Duration, Description, Bill, Active, FirstName, MiddleName, LastName
    FROM xxxx.xxxx.vw_Hours_ServiceUsers
    WHERE FirstName LIKE @UserTxtFName AND LastName LIKE @UserTxtLName AND TaskDate BETWEEN @UserTxtFrDate AND @UserTxtToDate
    ORDER BY TaskDate, FirstName, LastName
END

If(@OrderBy = 'TLF')
BEGIN
    SELECT Project_FK, TaskID_FK, UserName_FK, CAST(TaskDate AS DATETIME2), Duration, Description, Bill, Active, FirstName, MiddleName, LastName
    FROM xxxx.xxxx.vw_Hours_ServiceUsers
    WHERE FirstName LIKE @UserTxtFName AND LastName LIKE @UserTxtLName AND TaskDate BETWEEN @UserTxtFrDate AND @UserTxtToDate
    ORDER BY FirstName, TaskDate, LastName
END

If(@OrderBy = 'None')
BEGIN
    SELECT Project_FK, TaskID_FK, UserName_FK, CAST(TaskDate AS DATETIME2), Duration, Description, Bill, Active, FirstName, MiddleName, LastName
    FROM xxxx.xxxx.vw_Hours_ServiceUsers
    WHERE FirstName LIKE @UserTxtFName AND LastName LIKE @UserTxtLName AND TaskDate BETWEEN @UserTxtFrDate AND @UserTxtToDate
END

SET NOCOUNT OFF
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

GO

VB代码:

Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

    ' Collect user input and set to variables
    Dim sb As New StringBuilder
    Dim UserTxtFName As String = Trim("'" + txtFName.Text + "%'")
    Dim UserTxtLName As String = Trim("'" + txtLName.Text + "%'")
    Dim UserTxtFrDate As Date = DateTime.Parse(Trim(txtFrmDate.Text))
    Dim UserTxtToDate As Date = DateTime.Parse(Trim(txtToDate.Text))
    Dim strOrderBy As String

' "Collapse" search box panels
    With Page
        grpSearch.Visible = False
        grpSort.Visible = False
        btnGrpShowHide.Text = "Show"
        btnSortShowHide.Text = "Show"
    End With

' Setting value of strOrderBy
    If drpFirstSort.SelectedValue = "LN" And drpSecSort.SelectedValue = "FN" And drpThirdSort.SelectedValue = "TD" Then
        'strOrderBy = "ORDER BY LastName, FirstName, TaskDate"
        strOrderBy = "LFT"
    ElseIf drpFirstSort.SelectedValue = "LN" And drpSecSort.SelectedValue = "TD" And drpThirdSort.SelectedValue = "FN" Then
        'strOrderBy = "ORDER BY LastName, TaskDate, FirstName"
        strOrderBy = "LTF"
    ElseIf drpFirstSort.SelectedValue = "FN" And drpSecSort.SelectedValue = "LN" And drpThirdSort.SelectedValue = "TD" Then
        'strOrderBy = "ORDER BY FirstName, LastName, TaskDate"
        strOrderBy = "FLT"
    ElseIf drpFirstSort.SelectedValue = "FN" And drpSecSort.SelectedValue = "TD" And drpThirdSort.SelectedValue = "LN" Then
        'strOrderBy = "ORDER BY FirstName, TaskDate, LastName"
        strOrderBy = "FTL"
    ElseIf drpFirstSort.SelectedValue = "TD" And drpSecSort.SelectedValue = "FN" And drpThirdSort.SelectedValue = "LN" Then
        'strOrderBy = "ORDER BY TaskDate, FirstName, LastName"
        strOrderBy = "TFL"
    ElseIf drpFirstSort.SelectedValue = "TD" And drpSecSort.SelectedValue = "LN" And drpThirdSort.SelectedValue = "FN" Then
        'strOrderBy = "ORDER BY TaskDate, LastName, FirstName"
        strOrderBy = "TLF"
    Else
        strOrderBy = "None"
    End If

' Establish connection, pass in parameters, execute stored procedure, and bind to GridView
    Dim ds As New DataSet("ServiceHours")
    Using conn As New SqlConnection("data source=100.10.10.10,50001;initial catalog=xxxx;user id=xxxReadOnly;Password=xxx##;Integrated Security=False;")
        Dim sqlComm As New SqlCommand("xxxx.spGetFromHours_ServiceUsers", conn)
        sqlComm.CommandType = CommandType.StoredProcedure
        Dim parUserTxtFName As SqlParameter = sqlComm.Parameters.AddWithValue("@UserTxtFName", UserTxtFName)
        Dim parUserTxtLName As SqlParameter = sqlComm.Parameters.AddWithValue("@UserTxtLName", UserTxtLName)
        Dim parUserTxtFrDate As SqlParameter = sqlComm.Parameters.Add("@UserTxtFrDate", System.Data.SqlDbType.DateTime)
        parUserTxtFrDate.Value = UserTxtFrDate
        Dim parUserTxtToDate As SqlParameter = sqlComm.Parameters.Add("@UserTxtToDate", System.Data.SqlDbType.DateTime)
        parUserTxtToDate.Value = UserTxtToDate
        Dim parOrderBy As SqlParameter = sqlComm.Parameters.AddWithValue("@OrderBy", strOrderBy)
        Dim da As New SqlDataAdapter()
        da.SelectCommand = sqlComm
        da.Fill(ds)
        Dim str1 As String = ds.Tables(0).Rows.Count

        GridView1.DataSource = ds.Tables(0)
        GridView1.DataBind()
    End Using

End Sub

ASP.NET代码:

<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="CSS_GridView01._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>ASP.NET Project</h1>
            </hgroup>
        </div>
    </section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <asp:Label runat="server" ID="lblGrpSearch" Text="Search Criteria:" />
    <asp:Panel runat="server" ID="grpSearch" BorderStyle="Solid" BorderWidth="1px" BorderColor="Silver">
        <asp:Label runat="server" ID="lblLName" Text="Last Name: " />
        <asp:TextBox runat="server" ID="txtLName" />
        <br /><br />
        <asp:Label runat="server" ID="lblFName" Text="First Name: " />
        <asp:TextBox runat="server" ID="txtFName" />
        <br /><br />
        <asp:Label runat="server" ID="lblFrmDate" Text="From Date: " />
        <asp:TextBox runat="server" ID="txtFrmDate" Text="2010-01-01" />
        <asp:Label runat="server" ID="lblToDate" Text="To Date: " />
        <asp:TextBox runat="server" ID="txtToDate" Text="2014-12-31" />
    </asp:Panel>
    <asp:Button runat="server" ID="btnGrpShowHide" Text="Hide" Font-Size="Smaller" Height="28px" />
    <br />
    <asp:Label runat="server" ID="lblGrpSort" Text="Sort Order:" />
    <asp:Panel runat="server" ID="grpSort" BorderStyle="Solid" BorderWidth="1px" BorderColor="Silver">
        <asp:Label runat="server" ID="lblSort1" Text="First Sort: " />
        <asp:DropDownList runat="server" ID="drpFirstSort">
            <asp:ListItem Text="-None-" Value="None"/>
            <asp:ListItem Text="Last Name" Value="LN" Selected="True" />
            <asp:ListItem Text="First Name" Value="FN" />
            <asp:ListItem Text="Task Date" Value="TD" />
        </asp:DropDownList>
        <br /><br />
        <asp:Label runat="server" ID="lblSort2" Text="Second Sort: " />
        <asp:DropDownList runat="server" ID="drpSecSort">
            <asp:ListItem Text="-None-" Value="None" />
            <asp:ListItem Text="Last Name" Value="LN" />
            <asp:ListItem Text="First Name" Value="FN" Selected="True" />
            <asp:ListItem Text="Task Date" Value="TD" />
        </asp:DropDownList>
        <br /><br />
        <asp:Label runat="server" ID="lblSort3" Text="Third Sort: " />
        <asp:DropDownList runat="server" ID="drpThirdSort">
            <asp:ListItem Text="-None-" Value="None" />
            <asp:ListItem Text="Last Name" Value="LN" />
            <asp:ListItem Text="First Name" Value="FN" />
            <asp:ListItem Text="Task Date" Value="TD" Selected="True"  />
        </asp:DropDownList>
    </asp:Panel>
    <asp:Button runat="server" ID="btnSortShowHide" Text="Hide" Font-Size="Smaller" Height="28px" />
    <br />
    <asp:Button runat="server" ID="btnSearch" Text="Search" />
    <br />
    <asp:GridView runat="server" ID="GridView1" AllowSorting="true" Width="100%" AutoGenerateColumns="False" EnableViewState="false" EnableViewStateMac="False" Enabled="False" Visible="False">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>.
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Right" />
                <ItemStyle Width="5%" />
            </asp:TemplateField>
            <asp:BoundField HeaderText="Project" DataField="Project_FK" ItemStyle-Width="10%" />
            <asp:BoundField HeaderText="Task ID" DataField="TaskID_FK" ItemStyle-Width="10%" />
            <asp:BoundField HeaderText="Username" DataField="UserName_FK" ItemStyle-Width="10%" />
            <asp:BoundField HeaderText="Task Date" DataField="TaskDate2" ItemStyle-Width="10%" />
            <asp:BoundField HeaderText="Duration" DataField="Duration" ItemStyle-Width="10%" />
            <asp:BoundField HeaderText="Description" DataField="Description" ItemStyle-Width="15%" />
            <asp:BoundField HeaderText="Bill" DataField="Bill" ItemStyle-Width="5%" />
            <asp:BoundField HeaderText="Active" DataField="Active" ItemStyle-Width="5%" />
            <asp:BoundField HeaderText="First Name" DataField="FirstName" ItemStyle-Width="10%" />
            <asp:BoundField HeaderText="Middle Name" DataField="MiddleName" ItemStyle-Width="10%" />
            <asp:BoundField HeaderText="Last Name" DataField="LastName" ItemStyle-Width="10%" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource runat="server" ID="SqlDataSource1" providerName="System.Data.SqlClient" />
</asp:Content>

1 个答案:

答案 0 :(得分:1)

我找到了解决方案!当我收集用户输入并将其设置为变量时,问题出现在我的VB代码中:

' Collect user input and set to variables
Dim sb As New StringBuilder
Dim UserTxtFName As String = Trim("'" + txtFName.Text + "%'")
Dim UserTxtLName As String = Trim("'" + txtLName.Text + "%'")
Dim UserTxtFrDate As Date = DateTime.Parse(Trim(txtFrmDate.Text))
Dim UserTxtToDate As Date = DateTime.Parse(Trim(txtToDate.Text))
Dim strOrderBy As String

在删除附加的单引号和模数后,我得到了我的结果集。显然,当您传入varchar参数时,会为您追加单引号

' Collect user input and set to variables
Dim sb As New StringBuilder
Dim UserTxtFName As String = Trim(txtFName.Text)
Dim UserTxtLName As String = Trim(txtLName.Text)
Dim UserTxtFrDate As Date = DateTime.Parse(Trim(txtFrmDate.Text))
Dim UserTxtToDate As Date = DateTime.Parse(Trim(txtToDate.Text))
Dim strOrderBy As String

我将不得不尝试对存储过程本身的模数进行硬编码。

我希望这有助于其他人。