I am trying to programatically generate buttons and add event handlers to them and i've succeeded in creating the buttons but when I click on one of the I get the following message object reference not set to an instance of an object .
Here is the code that creates the buttons:
Dim i As Integer = 1
For Each dtab As DataTable In ds.Tables
For Each drow As DataRow In dtab.Rows
If (CInt(drow(3)) < CInt(drow(4))) Then
Dim l As New Label()
With l
.Location = New System.Drawing.Point(30, 40 * i)
.Text = "product number " & drow(0) & " has reached the minimal quantity"
.Width = 300
Panel3.Controls.Add(l)
End With
Dim b As New Button()
With b
.Location = New System.Drawing.Point(350, 40 * i)
.Enabled = True
.Width = 100
.Text = "fill stock"
Panel3.Controls.Add(b)
AddHandler b.Click, AddressOf Me.Button_Click
End With
i = i + 1
End If
Next
Next
the code that handels the button click is the following:
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim co As New SqlConnection("server=JAMMINGJACK-PC\SQLEXPRESS ;initial catalog=td3;integrated security=true;")
Try
Dim cmd As New SqlCommand
co.Open()
cmd.Connection = co
cmd.CommandText = "Update article set qteStck=(qteStck+(seuilmin*3)) where numart=" & drow(0) '
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
co.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
End Try
End Sub