How can I insert an image from SQL Server onto a button in VB.net? - sql

I am having issues assigning an image to every button according to my database.
My current code:
Dim strsql As String
Dim ImgSQl As Image
Dim con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Image FROM Inventory WHERE ID=#ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("#ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSQl = myreader("Image")
con.Close()
btn1.Image = ImgSQl
This is the error I'm getting:
Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'

All I had to do was dim as Byte, also adding "()" is really important for it to work. Hope answering my own question helps a VB.net beginner out there.
Dim strsql As String
Dim ImgSql() As Byte
Using con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Image FROM Inventory WHERE ID=#ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("#ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSql = myreader("Imagen")
Dim ms As New MemoryStream(ImgSql)
btn1.Image = Image.FromStream(ms)
con.Close()
End Using

Related

How to check if byte is null vb.net

I'm assigning background images to every button according to my SQL Server database. If a byte is null I get this error:
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
I want to allow buttons with no background images, but the error is preventing me.
Here is what I attempted:
Dim strsql As String
Dim ImgSql() As Byte
Using con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Imagen FROM Inventario WHERE ID=#ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("#ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSql = myreader("Imagen")
If ImgSql IsNot Nothing AndAlso ImgSql.Length > 0 Then
Dim ms As New MemoryStream(ImgSql)
btn1.BackgroundImage = Image.FromStream(ms)
con.Close()
Else
'do nothing
End If
End Using
Dim ItemID As Integer = 1
Dim sql As String = "SELECT Imagen FROM Inventario WHERE ID=#ID"
Using con As New SqlConnection("constring"), _
cmd As New SqlCommand(sql, con)
cmd.Parameters.Add("#ID", SqlDbType.Integer).Value = ItemID
con.Open()
Using myreader As SqlDataReader = cmd.ExecuteReader()
If myreader.Read() AndAlso Not DBNull.Value.Equals(myreader("Imagen")) Then
Dim ImgSql() As Byte = DirectCast(myreader("Imagen"), Byte())
Using ms As New MemoryStream(ImgSql)
btn1.BackgroundImage = Image.FromStream(ms)
End Using
End If
End Using
End Using
Based on this answer in C#, converted to vb.net and added NULL check
Using con As New SqlConnection("constring")
Using cmd = con.CreateCommand()
cmd.CommandText = "SELECT Imagen FROM Inventario WHERE ID=#ID"
Dim ItemID = 1
cmd.Parameters.AddWithValue("#ID", ItemID)
con.Open()
Dim res = cmd.ExecuteScalar()
If res IsNot Nothing Then
ImgSql = CType(res, Byte())
If ImgSql IsNot Nothing AndAlso ImgSql.Length > 0 Then
Dim ms As New MemoryStream(ImgSql)
btn1.BackgroundImage = Image.FromStream(ms)
End If
End If
End Using
End Using

How do I get multiple values from query using OleDBConnection?

I have edited the previous code and tried this below, I have also changed the textbox into a listbox with the same name, but I now get no value in the listbox after running the below code:
myConnection.ConnectionString = providerEdit
Dim str As String
str = "SELECT [Email] FROM [PRD_Records] WHERE [ReceiveKMCWEMSAlerts] = Yes"
Using cmd As OleDbCommand = New OleDbCommand(str, myConnection)
myConnection.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While reader.Read()
txtCreateAnnTo.Text = reader(0).ToString
End While
reader.Close()
End Using
Thanks everyone for your responses.... I found the problem with the code, it was very simple and I was just overlooking it. I update the coded below: myConnection.ConnectionString = providerEdit
Dim str As String
str = "SELECT [Email] FROM [PRD_Records] WHERE [ReceiveKMCWEMSAlerts] = Yes"
Using cmd As OleDbCommand = New OleDbCommand(str, myConnection)
myConnection.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While reader.Read()
txtCreateAnnTo.Items.Add(reader(0).ToString)
End While
reader.Close()
End Using

Storing multiple values from SQL Server statement

Consider the following code
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = #username and UserPass = #password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
Dim da As New SqlDataAdapter(cmd)
Suppose that there exist a column name status I want to store the result of status in a variable.
P.S I am a beginner with VB.NET so please bear with me
You can execute the statement on a SqlDataReader or fill DataTable. An example with SqlDataReader is
Dim reader As SqlDataReader
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName =#username and UserPass=#password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
reader = cmd.ExecuteReader()
Dim strStatus as String = ""
If reader.HasRows Then
reader.Read()
strStatus = reader.Item("status").ToString
End If
Here is the DataTable version
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName =#username and UserPass=#password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
Dim da As SqlDataAdapter = New SqlDataAdapter()
Dim dt As DataTable = New DataTable("TableA")
da.SelectCommand = cmd
da.Fill(dt)
Dim strStatus as String = ""
'you can process the DataTable in a for/for each loop or process a single row as follows
If dt.Rows.Count > 0 Then
strStatus = dt.Rows(0).Item("status").ToString()
End If
If status returns a single cell value(single row in status column) then you can use following method
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = #username and UserPass = #password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
Dim status As String
status = IIf(IsDBNull(cmd.ExecuteScalar), "Not Available", cmd.ExecuteScalar)
'IsDBNull is used tocheck whether cmd.ExecuteScalar is null or what
'IIF() is used to handle null here, You can assign custom value for status variable if
'select returns null value
'If you dont need to use them its possible to write
' status = cmd.ExecuteScalar
See more about ExecuteScalar()

vb.net delete from access database

I'm trying to simply delete data from my access database on the click of a button..
But can't get it to work.
Dim Cmd2 As OleDbCommand
Dim SQL2 As String
Dim objCmd2 As New OleDbCommand
Dim Con2 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Privat\dreamware\dreamware_db.mdb")
SQL2 = "DELETE FROM dreamware_db WHERE id='21'"
Cmd2 = New OleDbCommand(SQL2, Con2)
Con2.Open()
objCmd2 = New OleDbCommand(SQL2, Con2)
objCmd2.ExecuteNonQuery()
Con2.Close()
Can someone spot the error?
I write your method without errors:
Dim SQL As String
Dim objCmd As OleDbCommand
Dim Con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Privat\dreamware\dreamware_db.mdb")
SQL = "DELETE FROM dreamware_db WHERE id='21'"
Con.Open()
objCmd = New OleDbCommand(SQL, Con)
objCmd.ExecuteNonQuery()
Con.Close()

Declare a DataSet for DataGridView CellFormatting

Im trying to change a row color in datagridview but the code that used to work for me needs a dataset. This is the code that i use to fill my datagridview
Dim sql As String
sql = "SELECT * FROM [product info]"
Dim adapter As New OleDbDataAdapter(sql, strcon)
Dim dt As New DataTable("[product info]")
adapter.Fill(dt)
DataGridView1.DataSource = dt
Dim sql1 As String
sql1 = "SELECT * FROM [product info]"
Dim adapter1 As New OleDbDataAdapter(sql1, strcon)
Dim cmd As New OleDbCommand(sql1, strcon)
strcon.Open()
Dim myreader As OleDbDataReader = cmd.ExecuteReader
myreader.Read()
strcon.Close()
How can i declare a dataset within this line of codes?
Dim sql As String
sql = "SELECT * FROM [product info]"
Dim adapter As New OleDbDataAdapter(sql, strcon)
Dim ds As New DataSet("[product info]")
adapter.Fill(ds)
DataGridView1.DataSource = ds
Dim sql1 As String
sql1 = "SELECT * FROM [product info]"
Dim adapter1 As New OleDbDataAdapter(sql1, strcon)
Dim cmd As New OleDbCommand(sql1, strcon)
strcon.Open()
Dim myreader As OleDbDataReader = cmd.ExecuteReader
myreader.Read()
strcon.Close()
Dim ds as New Dataset("product_info")
ds.Tables.Add(dt)
Add these row after you filled data to datatable dt