2013-12-16 74 views
2

Im強制使用ODBC,我想將DataSet綁定到FormView。到目前爲止,我有這樣的代碼:將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?還是有更好的方法?

回答

1

我注意到你可能需要在這裏改變一些東西。以下是您修改後的功能。主要問題是連接沒有關閉。無論您在別處讀取什麼內容,SQL連接都不會被Using塊關閉。但是,我不確定OdbcConnection。我會建議包括結束線以防萬一。

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) 
      connection.Close() 
     Catch ex As Exception 
      connection.Close() 'This is necessary to avoid accidental multiple connections. 
     End Try 
     ' The connection is automatically closed when the 
     ' code exits the Using block. 
    End Using 

    Return dataSet 
End Function 

但是我認爲真正的問題是你沒有引用表本身。數據集只包含了表,則必須結合時指定他們...

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.Tables(0) 
    FormView1.DataBind() 

End Sub 
+0

感謝您的快速回答,但它沒有什麼區別,FormView控件仍是空白,當我使用修改過的功能。 – user1359448

+0

@ user1359448你有沒有嘗試在數據綁定上放置一個斷點?如果沒有,那麼確保表格中有行和數據。 – Lopsided

+0

不,我不是在本地開發,代碼託管,並通過FTP工作。如果我直接從管理工作室運行查詢,我會返回數據。 – user1359448