Hi Im working on a crud project and I have an error Showing a message: An unhandled exception of type 'System.TypeInitializationException' occurred in VbMySQL_SMART_CRUD.exe
and the yellow area poiting to this : sql = "SELECT auto_id, first_name, last_name, CONCAT(first_name, ' ', last_name) AS 'full_name', gender FROM tbl_smart_crud" &
"WHERE CONCAT(first_name, ' ', last_name) LIKE @keyword1 OR gender = @keyword2 ORDER BY auto_id ASC"
Here is the full code and tell me what is wrong here please:
Option Explicit On
Option Strict On
Imports MySql.Data.MySqlClient
Public Class Form1
Private ID As String = ""
Private intRow As Integer = 0
Private Sub Form1_Load(sender As Object, e As
EventArgs) Handles MyBase.Load
ResetMe()
LoadData()
End Sub
Private Sub ResetMe()
Me.ID = ""
FirstNameTextBox.Text = ""
LastNameTextBox.Text = ""
If GenderComboBox.Items.Count > 0 Then
GenderComboBox.SelectedIndex = 0
End If
UpdateButton.Text = "Update()"
DeleteButton.Text = "Delete()"
KeywordTextBox.Clear()
KeywordTextBox.Select()
End Sub
Private Sub Execute(Mysql As String, Optional Parameter As String = "")
cmd = New MySqlCommand(Mysql, con)
Addparameters(Parameter)
performcrud(cmd)
End Sub
Private Sub Addparameters(str As String)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("FirstName", FirstNameTextBox.Text.Trim())
cmd.Parameters.AddWithValue("LastName", LastNameTextBox.Text.Trim())
cmd.Parameters.AddWithValue("Gender", GenderComboBox.SelectedItem.ToString())
If str = "Update" Or str = "Delete" And Not String.IsNullOrEmpty(Me.ID) Then
cmd.Parameters.AddWithValue("ID", Me.ID)
End If
End Sub
Private Sub InsertButton_Click(sender As Object, e As EventArgs) Handles InsertButton.Click
If String.IsNullOrEmpty(FirstNameTextBox.Text.Trim()) Or String.IsNullOrEmpty(LastNameTextBox.Text.Trim()) Then
MsgBox("Por Favor Escreva As Informa?oes Em Falta. ", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation,
"Nova Estrela - Dados Do Produto")
Exit Sub
End If
sql = "INSERT INTO tbl_smart_crud(first_name, last_name, gender) VALUES(@FirstName, @LastName, @Gender)"
Execute(sql, "Insert")
MsgBox("Muito Obrigado... O registro Acima Foi Salvo Com Exito! ", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
"Nova Estrela - Dados Do Produto")
LoadData()
ResetMe()
End Sub
Private Sub LoadData(Optional keyword As String = "")
sql = "SELECT auto_id, first_name, last_name, CONCAT(first_name, ' ', last_name) AS 'full_name', gender FROM tbl_smart_crud" &
"WHERE CONCAT(first_name, ' ', last_name) LIKE @keyword1 OR gender = @keyword2 ORDER BY auto_id ASC"
Dim strKeyword As String = String.Format("%{0}%", keyword)
cmd = New MySqlCommand(sql, con)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("keyword1", strKeyword)
cmd.Parameters.AddWithValue("keyword2", keyword)
Dim dt As DataTable = performcrud(cmd)
If dt.Rows.Count > 0 Then
intRow = Convert.ToInt32(dt.Rows.Count.ToString())
Else
intRow = 0
End If
ToolStripStatusLabel1.Text = "Numero De Registro(s): " & intRow.ToString()
With DataGridView1
.MultiSelect = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.AutoGenerateColumns = True
.DataSource = dt
.Columns(0).HeaderText = "ID"
.Columns(0).HeaderText = "FirstName"
.Columns(0).HeaderText = "LastName"
.Columns(0).HeaderText = "FullName"
.Columns(0).HeaderText = "Preco De Compra"
.Columns(0).HeaderText = "Gender"
.Columns(0).Width = 85
.Columns(0).Width = 170
.Columns(0).Width = 170
.Columns(0).Width = 220
.Columns(0).Width = 100
End With
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Try
Dim dgv As DataGridView = DataGridView1
If e.RowIndex <> -1 Then
Me.ID = Convert.ToString(dgv.CurrentRow.Cells(0).Value).Trim()
UpdateButton.Text = "Update(" & Me.ID & ")"
DeleteButton.Text = "Delete(" & Me.ID & ")"
FirstNameTextBox.Text = Convert.ToString(dgv.CurrentRow.Cells(1).Value).Trim()
LastNameTextBox.Text = Convert.ToString(dgv.CurrentRow.Cells(2).Value).Trim()
GenderComboBox.SelectedItem = Convert.ToString(dgv.CurrentRow.Cells(4).Value).Trim()
End If
Catch ex As Exception
End Try
End Sub
Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
If DataGridView1.Rows.Count = 0 Then
Exit Sub
End If
If String.IsNullOrEmpty(Me.ID) Then
MsgBox("Por Favor Escolha Um Produto ou Artigo Na Lista.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation,
"Nova Estrela - Actualizando Dados Do Produto")
Exit Sub
End If
If String.IsNullOrEmpty(FirstNameTextBox.Text.Trim()) Or String.IsNullOrEmpty(LastNameTextBox.Text.Trim()) Then
MsgBox("Por Favor Escreva As Informa?oes Em Falta. ", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation,
"Nova Estrela - Actualizando Dados Do Produto")
Exit Sub
End If
sql = "Update tbl_smart_crud Set first_name = @FirstName, last_name = @LastName, gender = @Gender WHERE auto_id = @ID"
Execute(sql, "Update")
MsgBox("O registro Acima Foi Actualizado Com Exito!", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
"Nova Estrela - Actualizando Dados Do Produto")
LoadData()
ResetMe()
End Sub
Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
If DataGridView1.Rows.Count = 0 Then
Exit Sub
End If
If String.IsNullOrEmpty(Me.ID) Then
MsgBox("Por Favor Escolha Um Produto ou Artigo Na Lista.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation,
"Nova Estrela - Apagando Dados e Registro Do Produto")
Exit Sub
End If
If MessageBox.Show("Queres Mesmo Deletar O Registro Seleccionado?", "Nova Estrela - Apagando Dados e Registro Do Produto",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
sql = "DELETE FROM add_product WHERE Codigo Do Produto = @Codigo Do Produto"
Execute(sql, "Delete")
MsgBox("O registro Foi Apagado Com Exito. ", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
"Nova Estrela - Apagando Dados e Registro Do Produto")
LoadData()
ResetMe()
End If
End Sub
Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
If Not String.IsNullOrEmpty(KeywordTextBox.Text.Trim()) Then
LoadData(KeywordTextBox.Text.Trim())
Else
LoadData()
End If
ResetMe()
End Sub
End Class
question from:
https://stackoverflow.com/questions/65877658/error-in-vb-net-mysql-crud-in-my-project