2015-09-30 34 views
0

我有點絕望,因此我爲什麼在這裏!我對編程相當陌生,並且已經給了一個任務,我需要使用一系列SQL查詢來生成一個簡單的HTML報表。還有一個用戶輸入,他們從組合框中選擇ClinicID並單擊一個按鈕來生成報告。採取組合框SelectedIndex數據和使用SELECT查詢 - VB.net

基本上,我有一個組合框,我已經填充了'ClinicID',如下所示。我也確保SelectedIndex正在工作。我需要以某種方式在SQL查詢方法中使用它,我也在下面提供了這個方法。 '?': -

Public Class frmReport1 
'Set lsData for Clinics table 
Dim lsData As List(Of Hashtable) 


'On form load 
Private Sub frmReport1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    cboClinicID.DropDownStyle = ComboBoxStyle.DropDownList 

    'Instantiate new ClinicController object 
    Dim cController As ClinicController = New ClinicController 

    'Load ClinicID 
    lsData = cController.findId() 

    For Each clinic In lsData 
     cboClinicID.Items.Add(CStr(clinic("ClinicID"))) 
    Next 

End Sub 

'Selected Index 
Private Sub cboClinicID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClinicID.SelectedIndexChanged 
    Dim selectedIndex As Integer = cboClinicID.SelectedIndex 
    Dim selectedItem As Object = cboClinicID.SelectedItem 

    'Print in debug window 
    Debug.Print("Selected clinicID: " & selectedItem.ToString()) 
    Debug.Print("Selected clinicID index: " & selectedIndex.ToString()) 

    Dim htData = lsData.Item(selectedIndex) 


End Sub 

SQL查詢方法**注意,我是從兩個不同的表拉動

凡是,我認爲我必須在'SelectedItem'中工作,但我不知道如何!

預期結果:用這三個選定字段輸出的html表。

Public Class ClinicOrderController 

Public Const CONNECTION_STRING As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PharmDB.accdb" 

'Dim cController As ClinicController = New ClinicController 
'Dim oController As OrderController = New OrderController 

Public Function findClinicOrder() As List(Of Hashtable) 

    'Instantiates a connection object 
    Dim oConnection As OleDbConnection = New OleDbConnection(CONNECTION_STRING) 
    'Instantiates a list of hashtables 
    Dim lsData As New List(Of Hashtable) 

    Try 
     Debug.Print("Connection string: " & oConnection.ConnectionString) 

     oConnection.Open() 
     Dim oCommand As OleDbCommand = New OleDbCommand 
     oCommand.Connection = oConnection 

     'Stored in the CommandText property of the command object 
     'SELECT SQL statement 
     oCommand.CommandText = "SELECT clinics.clinic_id, orders.date_ordered, orders.total_price FROM clinics, orders WHERE clinics.clinic_id = orders.clinic_id AND clinics.clinic_id = ? ORDER BY clinics.clinic_id" 

     'Compiles the prepared statement 
     'oCommand.Prepare() 
     'Executes the SQL statement and stores the results in data reader object 
     Dim oDataReader = oCommand.ExecuteReader() 

     'Process data set in Hashtable 
     Dim htTempData As Hashtable 
     Do While oDataReader.Read() = True 
      htTempData = New Hashtable 
      htTempData("ClinicID") = CStr(oDataReader("clinic_id")) 
      htTempData("DateOrdered") = CStr(oDataReader("date_ordered")) 
      htTempData("OrderTotalPrice") = CStr(oDataReader("total_price")) 
      lsData.Add(htTempData) 

     Loop 

     Debug.Print("The record was found.") 

    Catch ex As Exception 
     Debug.Print("ERROR:" & ex.Message) 
     MsgBox("An error occured!") 
    Finally 
     oConnection.Close() 
    End Try 

    'Return list of hashtables to the calling function 
    Return lsData 

End Function 

真的,真的很感謝這裏的任何幫助。我一直在爲此奮鬥超過8小時(不是開玩笑 - 我允許你笑)

+0

@Japz Divino的 欣賞響應。 不幸的是,我得到這段代碼的異常錯誤(這有助於生成HTML表格) - 錯誤是針對HtSample行的。 'code'Private功能generateTable(BYVAL lsData方式列表(哈希表中))作爲字符串 '生成表 昏暗穩定=開始 「<表邊界=」 「1」 「>」 &vbCrLf 昏暗htSample作爲Hashtable = lsData。項(0) 昏暗lsKeys方式列表(字符串)=新的列表(串) lsKeys.Add( 「ClinicID」) lsKeys.Add( 「DateOrdered」) lsKeys.Add( 「OrderTotalPrice」) '代碼' – antisonfire

+0

這不是什麼關於它(即詢問可能對未來讀者有幫助的具體問題)。爲了分析代碼,你應該去Code Review(http://codereview.stackexchange.com/)。無論如何,不​​清楚和期望人們浪費時間去理解和解決你的問題似乎並不是獲得幫助的最佳途徑。 – varocarbas

+0

這篇文章屬於Code Review。 – varocarbas

回答

0

如果我理解你的話,你想在你的WHERE條款中使用你的dropdown選定的項目。要達到此目的,請使用INNER JOINON修改您的加入,然後將您的過濾條件設置爲WHERE。以下的希望代碼將有所幫助

SELECT clinics.clinic_id, 
     , orders.date_ordered 
     , orders.total_price 
    FROM clinics INNER JOIN orders ON clinics.clinic_id = orders.clinic_id 
    WHERE clinics.clinic_id = selectedItem.ToString() 
    ORDER BY clinics.clinic_id 

如果selectedItem.ToString()沒有工作,你可以嘗試SelectedValue

+0

請考慮編輯您的帖子,以添加更多關於您的代碼的解釋以及爲什麼它可以解決問題。一個主要包含代碼的答案(即使它正在工作)通常不會幫助OP瞭解他們的問題。如果這只是猜測,也建議您不要發佈答案。一個好的答案會有一個合理的理由解釋爲什麼它可以解決OP的問題。 – SuperBiasedMan

+0

嘿,這個解決方案對我來說很有意義,而且是我之前嘗試的 - 但我不確定如何讓SELECT語句實際使用selectedItem值。這樣做似乎並不奏效。不過,請欣賞迴應。 – antisonfire

+0

@SuperBiasedMan後修改..我的答案是基於我對這個問題的理解不是一個猜測。 – Japongskie

0

假設clinic_id是在數據庫中的數字字段:(否則只用單引號括起來(「」))

string clinicID = cboClinicID.SelectedItem.ToString(); 
string sql = string.Format(@"SELECT clinics.clinic_id, orders.date_ordered, orders.total_price 
          FROM clinics, orders 
          WHERE clinics.clinic_id = orders.clinic_id 
          AND clinics.clinic_id = {0} 
          ORDER BY clinics.clinic_id", clinicID); 

oCommand.CommandText = sql; 

你也可以做這樣的:

string sql = "SELECT clinics.clinic_id, orders.date_ordered, orders.total_price " + 
      "FROM clinics, orders " + 
      "WHERE clinics.clinic_id = orders.clinic_id " + 
      "AND clinics.clinic_id = " + clinicID + " " + 
      "ORDER BY clinics.clinic_id"; 
-1

請公關在vb.net

這裏奧維德代碼是我的代碼,我想從表中顯示獎金金額,其中class id爲條件:

如果class id是3,那麼獎金爲這是在我的文本框顯示名爲txtprize.textclass Id是顯示在列表框中。

Private Sub listClassID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listClassID.SelectedIndexChanged 
Dim classIdlist As String 
classIdlist = New String(listClassID.SelectedItem.ToString) 
Dim strSQL As String = "select [Prize Amount] from Master_Class WHERE [Class ID] =" & classIdlist 
Dim dr As SqlDataReader 

Try 
    con.Open() 
    cmd = New SqlCommand(strSQL, con) 
    dr = cmd.ExecuteReader() 
    If dr.Item(0) Then 
     txtPrize.Text = dr("[Prize Amount]").ToString 
    End If 

    dr.Close() 
    cmd.Dispose() 
    con.Close() 

Catch ex As Exception 
    MsgBox(ex.Message) 
    con.Close() 
End Try 
End Sub