ASP.net mySQL Insert function
This code is from a football website off a Add_Player page. The code will add a new football player to the database.
The table the code is added to contains:
ID,Name, Team, DOB, Bio, Position, SquadNo, Birthplace
As ID is set to auto increment within the database then it is not required in the insert.
I have placed some very basic error checking, the TRY CATCH will attempt to insert the data, outputing to a label if it is successful or not.
code:
Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs)
       Dim myConnection As MySqlConnection
       Dim myDataAdapter As MySqlDataAdapter
       Dim myDataSet As DataSet
       myConnection = New MySqlConnection(”server=mysql.server.com; user id=Admin; password=xxx; database=data1; pooling=false;”)
       Dim myExecuteQuery = “INSERT INTO Players (Name, Team, DOB, Bio, Position, SquadNo, Birthplace) VALUES ( ‘” & txtname.Text & “‘ , ‘” & listTeam.selecteditem.value & “‘, ‘” & txtDOB.text & “‘ , ‘” & txtbio.text & “‘ ,’” & listPos.selecteditem.value & “‘ ,’” & txtsquadno.text & “‘,’” & txtbirth.text & “‘ )”
       Dim myCommand As New MySqlCommand(myExecuteQuery, myConnection)
       myCommand.Connection.Open()
       Try
           myCommand.ExecuteNonQuery()
           myConnection.Close()
           lblmsg.text = “Player has been added ”
       Catch
           lblmsg.text = “Could not insert please resubmit”
       End Try
   End Sub
























You must be logged in to post a comment.