ASP.net绑定使用3个连接连接到SQL数据库的字段

时间:2016-02-17 15:47:17

标签: asp.net sql-server

我有一个连接到SQL数据库的ASP搜索样式表单。我已经使用SQL找出了代码,我一直在尝试将它添加到我的ASP表单中。我尝试的所有内容都给了我错误“在所选数据源上找不到名称为'A.FirstNameFirst'的字段或属性”。 这是我的正面代码:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" Style="text-align: center" PageSize="2" Width="840px" ForeColor="#333333" EnableModelValidation="True">
      <Columns>
         <asp:BoundField  DataField="A.FirstNameFirst" HeaderText="Full Name" HeaderStyle-BackColor="#99CCFF" />
         <asp:BoundField DataField="B.ServiceLocation" HeaderText="Address" HeaderStyle-BackColor="#99CCFF"  />
         <asp:BoundField DataField="C.out_trans_dt" HeaderText="Bill Due Date" HeaderStyle-BackColor="#99CCFF"  />

      </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString2 %>"></asp:SqlDataSource>

这是我背后的代码:

Dim fnameSearchID, lnameSearchID, addressSearchID As TextBox
Dim searchItem As Boolean = False
Dim searchString As String

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    Try
        searchString = "SELECT A.FirstNameFirst AS 'Name', B.ServiceLocation AS 'Location', MAX(C.out_trans_dt) AS 'Bill Paid' From [database].[dbo].[vw_Simple_customer] AS A JOIN [database].[dbo].[vw_location] AS B ON B.customer_id=A.customer_id JOIN [database].[dbo].[ub_financial_tran_detail] AS C ON C.location_id=B.location_id"

        fnameSearchID = CType(DetailsView1.FindControl("fnameSearchID"), TextBox)
        lnameSearchID = CType(DetailsView1.FindControl("lnameSearchID"), TextBox)
        addressSearchID = CType(DetailsView1.FindControl("addressSearchID"), TextBox)

        If String.IsNullOrEmpty(fnameSearchID.Text) = False Then
            searchString += " WHERE A.FirstName LIKE '%" + fnameSearchID.Text + "%'"
            searchItem = True

        End If

        If String.IsNullOrEmpty(lnameSearchID.Text) = False Then
            If searchItem Then
                searchString += " AND A.Surname LIKE '%" + lnameSearchID.Text + "%'"
            Else
                searchString += " WHERE A.Surname LIKE '%" + lnameSearchID.Text + "%'"
                searchItem = True
            End If
        End If

        If String.IsNullOrEmpty(addressSearchID.Text) = False Then
            If searchItem Then
                searchString += " AND B.ServiceLocation LIKE '%" + addressSearchID.Text + "%'"
            Else
                searchString += " WHERE B.ServiceLocation LIKE '%" + addressSearchID.Text + "%'"
                searchItem = True
            End If
        End If

        searchString += " GROUP BY ServiceLocation, FirstNameFirst"

        Dim aSqlDataReader As SqlDataReader
        Dim aSqlConnection As SqlConnection = New SqlConnection("Server=apphost007;Initial Catalog=database;User ID=user;Password=password")
        Dim aSqlCommand As SqlCommand = New SqlCommand(searchString, aSqlConnection)
        aSqlConnection.Open()
        aSqlDataReader = aSqlCommand.ExecuteReader(CommandBehavior.CloseConnection)

        'Fill gridview with search results
        GridView1.DataSource = aSqlDataReader
        GridView1.AutoGenerateColumns = False
        GridView1.DataBind()

        'set fields read only
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
        searchCriteria.Visible = False
        searchResults.Visible = True

    Catch ex As Exception
        Response.Write("*** UNABLE TO SUBMIT SEARCH.*** " & ex.Message)
    End Try
End Sub

如果您能看到错误,请告诉我。

0 个答案:

没有答案