简体   繁体   English

VB.NET数据库数据传递错误

[英]VB.NET database data passing error

I get the error message 我收到错误消息

"Error while inserting record o the table… Incorrect syntax near the keyword 'return'.Incorrect syntax near ',' ." “在表中插入记录时出错...关键字'return'附近的语法不正确。','附近的语法不正确。”

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

Change this line: 更改此行:

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 & "')"

To this: 对此:

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 & "')"

Notice the square brackets around the table name. 请注意表格名称周围的方括号。 Return is a reserved word in SQL Server, which is causing your error. Return是SQL Server中的保留字,这会导致您出错。 I would recommend that you change the name of the table. 我建议您更改表的名称。 If this is not possible, then you are stuck adding the square brackets every time you use the table. 如果无法做到这一点,那么每次使用表格时都必须添加方括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM