2013-05-08 66 views
0

VB.NET數據庫中的數據出現錯誤消息傳遞錯誤

「錯誤,同時插入記錄Ø表...的 關鍵字附近有語法錯誤‘return'.Incorrect語法附近’,」。」

Public Class Form10 
    Dim con As New SqlClient.SqlConnection 
    Dim cmd, com As New SqlClient.SqlCommand 
    Dim sqlda As New SqlClient.SqlDataAdapter 
    Dim ds As New DataSet 


Private Sub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     Try 
      con.ConnectionString = "data source=.\sqlexpress;initial catalog =pharmacy;Integrated Security =true" 
     Catch ex As Exception 
      MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) 
     End Try 
    End Sub 

    Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click 
     txtdcode.Text = "" 
     txtrinvo.Text = "" 
     txtcreg.Text = "" 
     txtprice.Text = "" 
     txtqty.Text = "" 
     txttamount.Text = "" 
     DateTimePicker1.Text = "" 
    End Sub 

    Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click 
     con.Open() 
     Try 
      sqlda = New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM return where r_invoice_no='" & txtrinvo.Text & "'", con) 
      ds.Clear() 
      sqlda.Fill(ds, "return") 
      txtdcode.Text = ds.Tables("return").Rows(0).Item(0) 
      txtcreg.Text = ds.Tables("return").Rows(0).Item(1) 
      txtprice.Text = ds.Tables("return").Rows(0).Item(2) 
      txtqty.Text = ds.Tables("return").Rows(0).Item(3) 
      txttamount.Text = ds.Tables("return").Rows(0).Item(4) 
      DateTimePicker1.Text = ds.Tables("return").Rows(0).Item(5) 

      ProgressBar1.Visible = True 
      ProgressBar1.Minimum = 0 
      ProgressBar1.Maximum = 10000 
      ProgressBar1.Value = 0 

      Dim I As Integer 
      For I = ProgressBar1.Minimum To ProgressBar1.Maximum 
       ProgressBar1.Value = I 
      Next 

      ProgressBar1.Visible = False 

     Catch ex As IndexOutOfRangeException 
      MsgBox("Type correct Return Invoice Number to search.." & ex.Message, MsgBoxStyle.Critical, "TRY AGAIN") 
     Finally 
      con.Close() 
     End Try 
    End Sub 



Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click 
     con.Open() 
     Try 
      If txtdcode.Text = "" Or txtrinvo.Text = "" Or txtcreg.Text = "" Or txtprice.Text = "" Or txtqty.Text = "" Or txttamount.Text = "" Or DateTimePicker1.Text = "" Then 
       MsgBox("You must have to fill all the Book details.") 
      Else 

       Dim strInst As String = "INSERT INTO return(drug_code,c_reg_no,Sale_price,qty,Tot_amount,date,r_invoice_no)VALUES('" & txtdcode.Text & "','" & txtcreg.Text & "','" & txtprice.Text & "','" & txtqty.Text & "','" & txttamount.Text & "','" & DateTimePicker1.Text & "','" & txtrinvo.Text & "')" 
       Dim cmd_Insert As New System.Data.SqlClient.SqlCommand(strInst, con) 
       cmd_Insert.ExecuteNonQuery() 

       ProgressBar1.Visible = True 
       ProgressBar1.Minimum = 0 
       ProgressBar1.Maximum = 10000 
       ProgressBar1.Value = 0 

       Dim I As Integer 
       For I = ProgressBar1.Minimum To ProgressBar1.Maximum 
        ProgressBar1.Value = I 
       Next 

       MsgBox("New Recored has been Added", MsgBoxStyle.Information) 

       ProgressBar1.Visible = True 

       txtdcode.Text = "" 
       txtrinvo.Text = "" 
       txtcreg.Text = "" 
       txtprice.Text = "" 
       txtqty.Text = "" 
       txttamount.Text = "" 
       DateTimePicker1.Text = "" 

      End If 
     Catch ex As Exception 

      MessageBox.Show("Error while inserting record on table..." & ex.Message, "Error Inserting") 
     Finally 
      con.Close() 
     End Try 

    End Sub 



Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click 
     Try 
      con.Open() 
      com.Connection = con 
      com.CommandText = "UPDATE return SET drug_code ='" & txtdcode.Text & "',c_reg_no='" & txtcreg.Text & "',Sale_price='" & txtprice.Text & "',qty='" & txtqty.Text & "',Tot_amount='" & txttamount.Text & "',date='" & DateTimePicker1.Text & "'" 
      com.ExecuteNonQuery() 

      ProgressBar1.Visible = True 
      ProgressBar1.Minimum = 0 
      ProgressBar1.Maximum = 10000 
      ProgressBar1.Value = 0 

      Dim I As Integer 
      For I = ProgressBar1.Minimum To ProgressBar1.Maximum 
       ProgressBar1.Value = I 
      Next 

      MsgBox("Your rocord has been Updated", MsgBoxStyle.Information, "Update Records") 

      ProgressBar1.Visible = False 

      txtdcode.Text = "" 
      txtrinvo.Text = "" 
      txtcreg.Text = "" 
      txtprice.Text = "" 
      txtqty.Text = "" 
      txttamount.Text = "" 
      DateTimePicker1.Text = "" 

     Catch ex As Exception 
      MessageBox.Show("Error while updating password on table..." & ex.Message, "Insert Records") 

     Finally 
      con.Close() 
     End Try 
    End Sub 



Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click 
     Try 
      con.Open() 

      com.Connection = con 
      com.CommandText = "Delete From return Where r_invoice_no=" & txtrinvo.Text 
      com.ExecuteNonQuery() 

      ProgressBar1.Visible = True 
      ProgressBar1.Minimum = 0 
      ProgressBar1.Maximum = 10000 
      ProgressBar1.Value = 0 

      Dim I As Integer 
      For I = ProgressBar1.Minimum To ProgressBar1.Maximum 
       ProgressBar1.Value = I 
      Next 

      MsgBox("Records are deleted with Return Invoice Number..." & txtrinvo.Text, MsgBoxStyle.Information, "Record Deleted.") 

      ProgressBar1.Visible = False 

      txtdcode.Text = "" 
      txtrinvo.Text = "" 
      txtcreg.Text = "" 
      txtprice.Text = "" 
      txtqty.Text = "" 
      txttamount.Text = "" 
      DateTimePicker1.Text = "" 

     Catch ex As InvalidCastException 
      MessageBox.Show("Error while retrieving records on table..." & ex.Message, "Load Records") 

     Finally 
      con.Close() 
     End Try 
    End Sub 

End Class 
+0

你應該definetly concider SqlParameters。 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx – 2013-05-08 16:38:33

+0

返回是SQL Server中的保留字。嘗試在你的SQL中放置方括號。 [返回] – 2013-05-08 16:41:33

回答

1

改變這一行:

Dim strInst As String = "INSERT INTO return(drug_code,c_reg_no,Sale_price,qty,Tot_amount,date,r_invoice_no)VALUES('" & txtdcode.Text & "','" & txtcreg.Text & "','" & txtprice.Text & "','" & txtqty.Text & "','" & txttamount.Text & "','" & DateTimePicker1.Text & "','" & txtrinvo.Text & "')" 

要這樣:

Dim strInst As String = "INSERT INTO [return](drug_code,c_reg_no,Sale_price,qty,Tot_amount,date,r_invoice_no)VALUES('" & txtdcode.Text & "','" & txtcreg.Text & "','" & txtprice.Text & "','" & txtqty.Text & "','" & txttamount.Text & "','" & DateTimePicker1.Text & "','" & txtrinvo.Text & "')" 

聲明的方括號中的表名。返回是SQL Server中的一個保留字,它會導致您的錯誤。我建議你改變表的名字。如果這是不可能的,那麼你每次使用表格時都會添加方括號。

+0

+1用於更改表名。 – Neolisk 2013-05-08 17:51:52