将FormView绑定到DataSet

时间:2013-12-16 17:31:47

标签: asp.net vb.net dataset odbc

我被迫使用ODBC,我想将DataSet绑定到FormView。到目前为止我有这个代码:

Sub lookup(data As String, city As String)

    Dim query As String = "SELECT FIND_KORT_VEJ_FUL.STREET_NAME, FIND_KORT_VEJ_FUL.ZIPCODE, UXOR_CITY_DK.NAME AS cityName FROM UXOR_CITY_DK " & _
    "Join(FIND_KORT_VEJ_FUL) " & _
    "ON UXOR_CITY_DK.KOMMUNE_KODE=FIND_KORT_VEJ_FUL.MUNICIPALITY_CODE WHERE UXOR_CITY_DK.NAME = '" & city & "' " & _
    "LIMIT 5"

    Dim connectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ODBCDataConnectionString").ConnectionString

    Dim initialDataSet As New DataSet("initial")
    Dim dt As DataSet = GetDataSetFromAdapter(initialDataSet, connectionString, query)

    FormView1.DataSource = dt
    FormView1.DataBind()

End Sub

Public Function GetDataSetFromAdapter(ByVal dataSet As DataSet, ByVal connectionString As String, ByVal queryString As String) As DataSet

    Using connection As New OdbcConnection(connectionString)
        Dim adapter As New OdbcDataAdapter(queryString, connection)

        ' Open the connection and fill the DataSet. 
        Try
            connection.Open()
            adapter.Fill(dataSet)
        Catch ex As Exception

        End Try
        ' The connection is automatically closed when the 
        ' code exits the Using block. 
    End Using

    Return dataSet
End Function

的FormView:

<asp:FormView ID="FormView1" runat="server" EmptyDataText="Ingen data">
            <ItemTemplate>
                <table>
                    <tr>
                        <td>Postnummer: <%#Eval("MUNICIPALITY_CODE") %></td>
                    </tr>
                    <tr>
                        <td>Indbyggere: </td>
                    </tr>
                    <tr>
                        <td>Geografisk lokation: </td>
                    </tr>
                    <tr>
                        <td>Roskilde ligger i <a href="#">Roskilde kommune</a></td>
                    </tr>
                    <tr>
                        <td><br /><h3><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></h3></td>
                    </tr>
                    <tr>
                        <td>Borgmester: </td>
                    </tr>
                    <tr>
                        <td>Antal veje i kommunen: #</td>
                    </tr>

                </table>
            </ItemTemplate>
        </asp:FormView>

我已经验证了查询和查询字符串,两者都有效。 FormView不会填充上面的代码。是否可以将FormView绑定到DataSet?还是有更好的方法?

0 个答案:

没有答案