Erreur 'System.NullReferenceException' + MySQL + Insert
Bonjour,
Je ne comprend pas pourquoi j'ai cette erreur. Est ce du a la base de donné ???
Il me le fait sur COMMAND.Connection = MysqlConn
Une piste ?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| Imports MySql.Data.MySqlClient
Public Class Producto
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Dim dr As MySqlDataReader
Private Sub Producto_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=database;Convert Zero Datetime=True"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox2.Text = "" Then
MessageBox.Show("Falta Introducir el Nombre de Producto")
ElseIf TextBox3.Text = "" Then
MessageBox.Show("Falta Introducir el Tipo de Producto")
ElseIf TextBox4.Text = "" Then
MessageBox.Show("Falta Introducir la Referencia de Producto")
Else
Try
MysqlConn.Open()
COMMAND.Connection = MysqlConn
COMMAND.CommandType = CommandType.Text
COMMAND.CommandText = "select * from database.Productos where id = '" & TextBox1.Text & "'"
dr = COMMAND.ExecuteReader
If dr.HasRows Then
MsgBox("DNI DUPLICADO !", MsgBoxStyle.Critical)
Me.Show()
MysqlConn.Close()
Else
MysqlConn.Close()
MysqlConn.Open()
COMMAND.CommandText = "insert into xCantera.Productos (nombre,tipo,ref) values ('" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
COMMAND.ExecuteReader()
MsgBox("Producto Guardado")
Controls.Clear()
InitializeComponent()
Producto_Load(Me, Nothing)
End If
Finally
MysqlConn.Dispose()
End Try
End If
End Sub
End Class |