I want to save data from textbox1 and textbox2 to database, but i get this error and the error do not mention which line that the error occur. This is the error:
Object reference not set to an instance of an object.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" And TextBox2.Text = "" Then Exit Sub
Try
conn = New MySqlConnection
conn.ConnectionString = connstr
conn.Open()
strsql = "INSERT INTO supplier "
strsql += "(SupplierName,SupplierID) VALUES "
strsql += "(@Name, @ID);"
cmd.Connection = conn
cmd.CommandText = strsql
cmd.Parameters.AddWithValue("@Name", TextBox1.Text)
cmd.Parameters.AddWithValue("@ID", TextBox2.Text)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
cmd.Dispose()
conn.Close()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub