How do i show images in ListView? - sql-server

Private Sub RETRIEVE()
ListView1.Clear()
Dim imglist As New ImageList
imglist.ColorDepth = ColorDepth.Depth32Bit
ListView1.LargeImageList = imglist
ListView1.LargeImageList.ImageSize = New System.Drawing.Size(50, 50)
Dim connection As New SqlConnection("Data Source=(local);Initial Catalog=images;Integrated Security=True")
Dim userdataadpter As New SqlDataAdapter("SELECT * FROM tbl_img ", connection)
Dim userdataset As New DataSet
Dim dt_images As New DataTable
userdataadpter.Fill(userdataset, "tbl_img")
For Each dr As DataRow In dt_images.Rows
Dim img_buffer = CType(dr("image"), Byte())
Dim img_stream As New MemoryStream(img_buffer, True)
img_stream.Write(img_buffer, 0, img_stream.Length)
imglist.Images.Add(dr("image").ToString(), New Bitmap(img_stream))
img_stream.Close()
Dim lsvpaarent As New ListViewItem
lsvpaarent.Text = dr("image").ToString
lsvpaarent.ImageKey = dr("imageID").ToString
ListView1.Items.Add(lsvpaarent)
Next
end sub

Looks like you're saving images as a byte array in your database and pulling them out, putting them into a imagelist, creating your listview items, then assigning the "ImageKey" to the "ImageId" of the imagelist?
The ImageKey should refer to the index of the item in the imagelist, not some id you have in a database.
See if that works for you.

Related

VB.NET synchronized Combobox and label from SQL Server

I'm trying to synchronize the reading of a SQL Server table with 2 columns (nom_unité and cout_unité).
The first column (nom_unité) will be populated into a combobox and I want the second column (cout_unité) to be synchronized into a label with the first combobox (meaning, when I change the combobox value, the label should change too refering to the table).
I can do this with 2 comboboxes :
Dim connection As New SqlConnection("Data Source=xxx")
Dim dt As New DataTable
Dim sqlquery As String
connection.Open()
sqlquery = "select * from liste_unités"
Dim SQL As New SqlDataAdapter(sqlquery, connection)
SQL.Fill(dt)
Dim cmd As New SqlCommand(sqlquery, connection)
ComboBoxC1L1.DataSource = dt
ComboBoxC1L1.DisplayMember = "nom_unité"
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "cout_unité"
but I do not know how to do it with a label (instead of ComboBox1).
I believe I can achieve it with something like that :
Dim sqlcmd As New SqlCommand("select * from liste_unités", connection)
Dim myreader As SqlDataReader
myreader = sqlcmd.ExecuteReader()
myreader.Read()
If myreader.HasRows Then
Label1.Text = myreader.Item("cout_unité").ToString
End If
but this is only reading the first row and not changing the label value when changing the first combobox selected value.
How to do it the easiest and most efficient way ?
Thank you :)
As you have assigned the datasource of the combobox to a datatable which contains the information you need, you can get that information when the value of the combobox changes.
I started a new Windows Forms project and put a combobox (named "cbNomUnité") and a label (named "lblCoutUnité") on Form1 and used this code:
Imports System.Data.SqlClient
Public Class Form1
Dim connStr As String = "Server=.\SQLEXPRESS;Database=Testing;Trusted_Connection=true;"
Sub PopulateCB()
Dim sql = "SELECT nom_unité, cout_unité FROM liste_unités"
Dim dt As New DataTable
Using conn As New SqlConnection(connStr),
da As New SqlDataAdapter(sql, conn)
da.Fill(dt)
End Using
cbNomUnité.DataSource = dt
cbNomUnité.DisplayMember = "nom_unité"
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbNomUnité.SelectedIndexChanged
Dim cb = DirectCast(sender, ComboBox)
If cb.SelectedIndex >= 0 Then
Dim val = DirectCast(cb.SelectedItem, DataRowView).Row.Field(Of String)("cout_unité")
lblCoutUnité.Text = val
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PopulateCB()
End Sub
End Class
To get a program that does this:
(Refresh the page to see the animation again.)

Filling multi tables into dataset?

I have problem with my code which fills multi tables into my dataset. It loads all contents contained in tables of my database to only one table in dataset. My code is shown below. How to load those tables from database into a dataset , that has the same number of tables and contents.
Private Sub Filldataset()
Private cnn As OleDbConnection
Private dt As New DataTable
Private da As New OleDbDataAdapter
Private cmd As New OleDbCommand
Private ds As New DataSet
Dim tblrestrictions As String() = New String() {Nothing, Nothing, Nothing, "TABLE"}
Dim userTables As DataTable = Nothing
userTables = cnn.GetSchema("Tables", tblrestrictions)
Dim i As Integer
For i = 1 To userTables.Rows.Count - 1 Step 1
cnn = New OleDbConnection(Str)
cnn.Open()
cmd = cnn.CreateCommand
cmd.CommandText = "select * from" & " " & userTables.Rows(i)(2).ToString
dt.Clear()
da.SelectCommand = cmd
da.Fill(dt)
da.Fill(ds)
Next
cnn.Close()
MessageBox.Show(ds.Tables.Count)
End Sub
Connections can be created elsewhere but should not be opened or closed until directly before an directly after you use them. You will have to adjust this code for an Oledb application.
Private Sub GetData()
cn.Open()
Dim dt As DataTable = cn.GetSchema("Tables")
cn.Close()
Dim ds As New DataSet
Dim row As DataRow
For Each row In dt.Rows
Dim strTableName As String = row(2).ToString
Dim strSQL As String = "Select * From " & strTableName
Dim cmd As New SqlCommand(strSQL, cn)
Dim da As New SqlDataAdapter
da.SelectCommand = cmd
da.Fill(ds, strTableName)
Next
Debug.Print(ds.Tables.Count.ToString)
End Sub
I scoped several variables locally that you will want to scope to the class like the dataset

Combobox Relation Table Did Not Show The Data

I've tried to add relation, to show the data on combobox and datagridview. I've tried this code below
Private Sub LoadSemester()
Me.OpenConn()
Dim dSet As New DataSet
Dim sql1 As String = "SELECT * FROM tbl_semester"
Dim comm1 As New SqlClient.SqlCommand(sql1, cnn)
Dim daSemester As SqlClient.SqlDataAdapter
Dim sql2 As String = "SELECT * FROM tbl_mk"
Dim comm2 As New SqlClient.SqlCommand(sql2, cnn)
Dim daMK As SqlClient.SqlDataAdapter
daMK = New SqlClient.SqlDataAdapter(comm2)
dSet.Clear()
daMK.Fill(dSet, "tbl_mk")
daSemester = New SqlClient.SqlDataAdapter(comm1)
dSet.Clear()
daSemester.Fill(dSet, "tbl_semester")
dSet.Relations.Add("relation", dSet.Tables("tbl_semester").Columns("id_pk"), dSet.Tables("tbl_pk").Columns("id_pk"))
With cmbSemester
.DataSource = dSet.Tables("tbl_semester")
.DisplayMember = "semester"
.ValueMember = "id_semester"
.SelectedIndex = 0
End With
'my datagridview here
End Sub
But it displayed nothing. But when I deleted the 'dSet.Relations.Add("relation", dSet.Tables("tbl_semester").Columns("id_pk"), dSet.Tables("tbl_pk").Columns("id_pk"))' the combobox showed the data.
I figured out maybe the problem is the dSet.Relation code. FYI, I am using SQLServer 2005 and VS Express 2012.

VB.NET listview not showing all data from dataset

This issue is driving me mad, I can't work out why it's happening. I'm running a query in an access db using vb.net then putting the data into a listview. Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strAccessConn As String = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = T:\mydb.accdb"
Dim cn As OleDbConnection = New OleDbConnection(strAccessConn)
Dim ds As New DataSet
Dim dt As DataTable
'Note the query is entered as a string.
Dim da As New OleDbDataAdapter("Q_LIST", cn) 'is the name of the query in Access
'Set the CommandType of the SelectCommand to TableDirect
da.SelectCommand.CommandType = CommandType.TableDirect
da.Fill(ds, "mytable")
dt = ds.Tables("mytable")
ListViewBatchResults.MultiSelect = True
ListViewBatchResults.View = View.Details
ListViewBatchResults.GridLines = True
ListViewBatchResults.Columns.Clear()
ListViewBatchResults.Items.Clear()
For Each col As DataColumn In dt.Columns()
ListViewBatchResults.Columns.Add(col.ToString)
Next
MsgBox(dt.Rows.Count)
For Each row As DataRow In dt.Rows()
Dim lst As ListViewItem
lst = ListViewBatchResults.Items.Add(row(0))
For i As Integer = 1 To dt.Columns.Count - 1
lst.SubItems.Add(row(i))
Next
Next
End Sub
This listview is not showing all the returned data though, and I can't work out why - it works on other queries in the DB but not this one for some reason. The row count shows that there are 264 rows, the listview only shows 3 of them when I run the project however. What the heck is going on?
Cheers!
Dim lst As ListViewItem
to
Dim lst As New ListViewItem

datagridview to textbox binding data

When the program is executed, the datagridview populate data and also the textboxes(example StockNumber,Description) and when i typed words in the search textbox ,the datagridview filters the matching words.When I clicked the item in the datagridview the textboxes does not changed it didnt show the information...
what the solution for my problem..i need to display the information in the textboxes when i clicked the item in the datagridview..
Private Sub txtreg_delsrch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtreg_delsrch.TextChanged
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\sony\Documents\Visual Studio 2008\Projects\Inventory\ItemInventory.mdb")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT StockNo,Item,Description,Reference,Quantity,Unit FROM Supplies_Regular WHERE Description Like '%" & txtreg_delsrch.Text & "%'", con)
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
Supplies_RegularDataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
End Sub
Perhaps you could use BindingSource:
Dim binding = New BindingSource()
With { .DataSource = myDataSet.Tables("MyTable") }
Supplies_RegularDataGridView1.DataSource = binding
StockNumber_textBox1.DataBindings.Add("Text", binding, "StockNo")
Last line simply binds your object's StockNo property to TextBox.Text.
You could do something like this in the Grid CellClick event.
Dim row As Integer = e.RowIndex
Dim col As Integer = e.ColumnIndex
textbox.Text = grid.Item(col, row).value

Resources