I was hoping someone could point me in the right direction with my current code. I'm getting an error while opening my program:
Data is Null. This method or property cannot be called on null values.
I put an else statement for btn.backgroundimage
but still get that error.
Here is what my code looks like:
Sub FillItems()
Try
con = New SqlConnection(cs)
con.Open()
Dim PictureCol As Integer = 1 ' the column # of the BLOB field
Dim cmdText1 As String = "SELECT RTRIM(ProductName),Image from Temp_Stock_Company INNER JOIN Product ON Product.PID=Temp_Stock_Company.ProductID where ShowPOS='Yes'"
cmd = New SqlCommand(cmdText1)
cmd.Connection = con
cmd.CommandTimeout = 0
rdr = cmd.ExecuteReader()
flpItems.Controls.Clear()
Do While (rdr.Read())
'Dim btn As New Button
'btn.Text = rdr.GetValue(0)
'btn.TextAlign = ContentAlignment.MiddleCenter
'btn.BackColor = Color.SteelBlue
'btn.ForeColor = Color.White
'btn.FlatStyle = FlatStyle.Popup
'btn.Width = 125
'btn.Height = 60
'btn.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
'UserButtons.Add(btn)
'flpItems.Controls.Add(btn)
Dim b(rdr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
rdr.GetBytes(PictureCol, 0, b, 0, b.Length)
Dim ms As New System.IO.MemoryStream(b)
Dim Dflp As New FlowLayoutPanel
Dflp.Size = New System.Drawing.Size(197, 197)
Dflp.BackColor = Color.Black
Dflp.BorderStyle = BorderStyle.None
Dflp.FlowDirection = FlowDirection.TopDown
Dim btn As New Button
Dim btnX As New Button
btn.Text = rdr.GetValue(0)
btn.Width = 197
btn.Height = 197
If DBNull.Value.Equals(rdr(1)) = False Then
btn.BackgroundImage = Image.FromStream(ms)
btn.BackgroundImageLayout = ImageLayout.Stretch
Else
btn.BackgroundImage = My.Resources._12
btn.BackgroundImageLayout = ImageLayout.Stretch
End If
btn.FlatStyle = FlatStyle.Flat
btn.FlatAppearance.BorderSize = 0
btn.Text = rdr.GetValue(0)
btn.Font = New System.Drawing.Font("Segoe UI Semibold", 1.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
btn.TextAlign = System.Drawing.ContentAlignment.BottomCenter
btn.ForeColor = System.Drawing.Color.Black
btnX.Text = rdr.GetValue(0)
btnX.FlatStyle = FlatStyle.Flat
btnX.Width = 0
btnX.Height = 0
btnX.FlatAppearance.BorderSize = 0
btnX.Text = rdr.GetValue(0)
btnX.Font = New System.Drawing.Font("Segoe UI Semibold", 8.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
btnX.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
btnX.ForeColor = Color.White
btnX.BackColor = Color.SteelBlue
UserButtons.Add(btn)
UserButtons.Add(btnX)
Dflp.Controls.Add(btn)
Dflp.Controls.Add(btnX)
flpItems.Controls.Add(Dflp)
AddHandler btn.Click, AddressOf Me.btnItems_Click
AddHandler btnX.Click, AddressOf Me.btnItems_Click
Loop
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End Sub