2013-10-21 43 views
1

我收到OLEDB例外是未處理的,我不能找到它爲什麼發生的任何人都可以注意到它爲我OLEDB例外是未處理

我粗體和斜體錯誤代碼

和異常顯示如下查詢表達式'Product Name =''Chair''中的語法錯誤(缺少運算符)。

感謝

Imports System.Data.OleDb 

Public Class Form1 

    'declare the veriables 

    Dim itemName As String 
    Dim itemprice, average, rows, index, totalPrice 
    Dim foundItem As Boolean 
    Dim conectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Education\BIT\Assainment\L1S2\VB\Assesment 2\Stock.accdb" 
    Dim reader As OleDbDataReader 
    Dim olDataConnection As New OleDbConnection(conectionString) 
    Dim olCommand As OleDbCommand 


    Private Sub Form1_Load(ByVal sender As System.Object, e As EventArgs) Handles MyBase.Load 
     'loding data to the combo box 

     totalPrice = 0 
     rows = 0 

     'start the connection 
     olDataConnection.Open() 
     olCommand = New OleDbCommand("SELECT * FROM Stock", olDataConnection) 
     reader = olCommand.ExecuteReader() 
     While (reader.Read()) 
      comDataStock.Items.Add(DirectCast(reader("Product Name"), String) & " -Rs. " & DirectCast(reader("Price"), Integer) & " /= ") 
      totalPrice = totalPrice + DirectCast(reader("Price"), Integer) 
      rows = rows + 1 
     End While 

     Try 
      average = totalPrice/rows 
      lblAverResult.Text = "Rs. " & average.ToString() & " /=" 
     Catch ex As Exception 
      MessageBox.Show("Somethingg Wrong ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information) 
     End Try 
     olDataConnection.Close() 

    End Sub 

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click 
     olDataConnection.Open() 
     olCommand = New OleDbCommand("DELETE FROM Stock WHERE Product Name=''" & comDataStock.SelectedItem.ToString().Split("-").GetValue(0).ToString() & "'", olDataConnection) 
      reader = olCommand.ExecuteReader() 
     olDataConnection.Close() 
     comDataStock.Items.Clear() 
     totalPrice = 0 
     rows = 0 

     olDataConnection.Open() 
     olCommand = (New OleDbCommand("SELECT FROM Stock ", olDataConnection)) 
     reader = olCommand.ExecuteReader() 

     While (reader.Read()) 
      comDataStock.Items.Add(DirectCast(reader("Price"), String) & " -Rs. " & DirectCast(reader("Price"), Integer) & " /= ") 
      totalPrice = totalPrice + DirectCast(reader("Price"), Integer) 
      rows = rows + 1 

     End While 

     Try 
      average = totalPrice/rows 
      lblAverResult.Text = " Rs. " & average.ToString() & " /=" 
     Catch ex As Exception 
      MessageBox.Show(ex.ToString()) 
     End Try 
     olDataConnection.Close() 

    End Sub 
End Class 
+0

設置在該線路上休息一下,看看是否所有的mangling工程以合法文本 – Plutonix

回答

2

當你有一個字段的名稱包含空格,如Product Name,你必須用方括號括起的名字。

olCommand = New OleDbCommand("DELETE FROM Stock WHERE [Product Name]='" & comDataStock.SelectedItem.ToString().Split("-").GetValue(0).ToString() & "'", olDataConnection) 
+1

滑稽,我的眼睛跳過就在這一點,直奔外來單引號只是爲了有權(但我看你抓那個也是)。 –

+0

對於我來說,我會錯過無關的引用,只是奇怪的語法突出顯示引起了我的注意。通過語法高亮保存。 :-) – HansUp

+0

@HansUp我改變了你說但仍然得到錯誤:( olCommand =新的OleDbCommand(「刪除股票在哪裏[產品名稱] =''」&comDataStock.SelectedItem.ToString()。拆分「)」。GetValue(0).ToString()&「'」,olDataConnection) reader = olCommand.ExecuteReader() –