Click here to Skip to main content
15,867,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB.NET
Imports MySql.Data.MySqlClient
Imports System.Data

Public Class Form2

    Public dbconn As MySqlConnection
    Public sql As String
    Public dbcomm As New MySqlCommand
    Public dbread As MySqlDataReader
    Public myadap As New MySqlDataAdapter
    Public mydata As New DataTable
    Public rowindex As Integer



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        dbconn = New MySqlConnection("Data Source=localhost;user id=root;password=[REMOVED];database=myconnector")


        Try
            dbconn.Open()
        Catch ex As Exception
            MsgBox("Connection Error: " & ex.Message.ToString)
        End Try
        sql = "INSERT INTO uservb(Book_name,Author_name,category,selling_price,stock) VALUES('" & aname.Text & "','" & bname.Text & "','" & ComboBox1.Text & "','" & dname.Text & "''" & cname.Text & "')"
        Try
            dbcomm = New MySqlCommand(sql, dbconn)
            dbread = dbcomm.ExecuteReader()
            dbread.Close()
            MsgBox("Data inserted.")
        Catch ex As Exception
            MsgBox("Failed to insert data: " & ex.Message.ToString())
        End Try
        mydata.Rows.Clear()
    End Sub

    Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        DataGridView1.Visible = True

        dbconn = New MySqlConnection("Data Source=localhost;user id=root;password=[REMOVED];database=myconnector")
        Try
            dbconn.Open()
        Catch ex As Exception
            MsgBox("Connection Error: " & ex.Message.ToString)
        End Try

        sql = "SELECT * FROM uservb"
        Try

            dbcomm.Connection = dbconn
            dbcomm.CommandText = sql

            myadap.SelectCommand = dbcomm
            myadap.Fill(mydata)
            DataGridView1.DataSource = mydata


            dbconn.Close()
        Catch ex As Exception
            MsgBox("Problem loading data: " & ex.Message.ToString)
        Finally
            dbconn.Dispose()

        End Try

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Form3.Show()



    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim stock As String = DataGridView1.SelectedRows(0).Cells(4).ToString


    End Sub


End Class


What I have tried:

I dont know what the mistake is can someone help!!
the new data is not adding in the database through vb
the problem is at 25 line
Posted
Updated 24-May-21 7:38am
v3
Comments
Dave Kreskowiak 24-May-21 13:12pm    
Next time, try not to post your passwords in the code you post.
Vinit Mistry 24-May-21 13:20pm    
i need answer why isnt data being inserted can u tell me that
Dave Kreskowiak 24-May-21 13:39pm    
Patience. People volunteer their time and I'm doing this while I'm in a meeting for work.
Vinit Mistry 24-May-21 14:14pm    
sorry i didnt mean to be rude

1 solution

There's multiple problems with what you're doing, not the least of which is building an SQL query string using string concatenation. DO NOT DO THAT! It leads to the possibility of your database being corrupted and even completely destroyed!

Use parameterized queries. Google for "vb.net parameterized query" to find out how.

Now, having said that, your use of string concatenation is also masking the problem causing the error message. Convert the code to a parameterized query and use the SqlParameter objects you create and you'll end up with much more readable code and the solution to your problem.

Take a real close look at your SQL statement. You're missing a comma near the end of the list of values:
... dname.Text & "''" & cname.Text
                  ^

Had you used a parameterized query, this would have been easy to spot:
sql = "INSERT INTO uservb (Book_name, Author_name, category, selling_price, stock)
VALUES (@bookName, @authorName, @category, @sellingPrice, @stock)"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900