I'm trying to load data from an Access database into a DataGridView.
This is my access database - Image has long binary data
However, when I retrieve the data from the database and try to load it into the DataGridView, it shows this error:
I have 2 forms, this one is for adding to database:
This one is for showing the database in the DataGridView
Here's my code to add my uploaded image to database.
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim breader As New BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()
Dim create As New OleDbCommand("INSERT INTO Officials ([officialname] , [age] , [birthdate] , [position] , [term], [status], [image] ) VALUES ('" & TextBox1.Text & "' , '" & TextBox2.Text & "' , '" & DateTimePicker1.Value & "' , '" & cb1 & "' , '" & TextBox3.Text & "' , '" & status & "' , #img )", con)
With create
.Parameters.Add("#on", OleDb.OleDbType.VarChar).Value = TextBox1.Text.Trim
.Parameters.Add("#age", OleDb.OleDbType.VarChar).Value = TextBox2.Text.Trim
.Parameters.Add("#bd", OleDb.OleDbType.VarChar).Value = DateTimePicker1.Value
.Parameters.Add("#pn", OleDb.OleDbType.VarChar).Value = cb1
.Parameters.Add("#tm", OleDb.OleDbType.VarChar).Value = TextBox3.Text.Trim
.Parameters.Add("#st", OleDb.OleDbType.VarChar).Value = status
.Parameters.Add("#img", OleDb.OleDbType.LongVarBinary).Value = imgbuffer
I can give you an example how to put images on a DataGridView from Access but you'll need to adapt to your reality.
Just add a DataGridView and create 2 columns, first as TextBoxColumn and second as ImageColumn.
The next step is to load data from access database, so use what you already have that is not shown in your post. It will be something like:
Dim GConn As New OleDbConnection("your connection string...")
Dim GCmd As New OleDbCommand()
Dim DtReader As OleDbDataReader
GCmd.Connection = GConn
GCmd.CommandText = "SELECT PhotoDescription, PhotoOLE FROM MY_TABLE;"
DtReader = GCmd.ExecuteReader ' DtReader will have all the rows from database
' For this test you need to load less than 100 records
dim iLine as integer=0
DataGridView1.rows.Add(100) ' add 100 rows to test
DtReader.Read ' read first record
Do
DataGridView1.Rows(iLine).Cells(0).Value=DtReader("PhotoDescription").ToString
DataGridView1.Rows(iLine).Cells(1).Value=CType(DtReader("PhotoOLE"), Byte())
iLine+=1
Loop while DtReader.Read
It's not necessary to store both birthdate and age, because one of them can be computed given a value for the other one.
You haven't provided enough code to identify the issue, but if the image data wasn't properly converted before storing it, that would cause an issue.
Below shows how to both insert and update data that contains an image, as well as how to retrieve the data. In the code below, you'll also find code that will create an Access database and a table.
Add a reference to Microsoft ADO Ext. 6.0 for DDL and Security
Note: This is required for the "CreateDatabase" function in the code below.
In VS menu, click Project
Select Add Reference...
Select COM
Check Microsoft ADO Ext. 6.0 for DDL and Security
The code is tested and fairly well-documented. Of particular importance are the following functions/methods:
GetImageAsByteArray
TblOfficialsExecuteNonQuery
TblOfficialsInsert
TblOfficialsGetData
Create a class (name: HelperAccess.vb)
Imports System.Data.OleDb
Imports System.IO
Public Class HelperAccess
Private _accessFilename As String = String.Empty
Private _connectionStr As String = String.Empty
Public ReadOnly Property AccessFilename
Get
Return _accessFilename
End Get
End Property
Sub New(accessFilename As String, Optional dbPassword As String = "")
'set value
_accessFilename = accessFilename
'create connection string
If Not String.IsNullOrEmpty(dbPassword) Then
_connectionStr = String.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0};Jet OLEDB:Database Password='{1}'", accessFilename, dbPassword)
Else
_connectionStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};", _accessFilename)
End If
End Sub
Public Function CreateDatabase() As String
Dim result As String = String.Empty
Dim cat As ADOX.Catalog = Nothing
Try
'create New instance
cat = New ADOX.Catalog()
'create Access database
cat.Create(_connectionStr)
'set value
result = String.Format("Status: Database created: '{0}'", _accessFilename)
Return result
Catch ex As Exception
'set value
result = String.Format("Error (CreateDatabase): {0}(Database: {1})", ex.Message, _accessFilename)
Return result
Finally
If cat IsNot Nothing Then
'close connection
cat.ActiveConnection.Close()
'release COM object
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat)
cat = Nothing
End If
End Try
End Function
Public Function CreateTblOfficials() As String
Dim result As String = String.Empty
Dim tableName As String = "Officials"
Dim sqlText = String.Empty
sqlText = "CREATE TABLE Officials "
sqlText += "(ID AUTOINCREMENT not null primary key,"
sqlText += " [FullName] varchar(50) not null,"
sqlText += " [Birthdate] DateTime,"
sqlText += " [JobDescription] varchar(50) not null,"
sqlText += " [Term] varchar(50),"
sqlText += " [Status] varchar(50) not null,"
sqlText += " [Photo] Longbinary);"
Try
'create database table
ExecuteNonQuery(sqlText)
result = String.Format("Table created: '{0}'", tableName)
Catch ex As OleDbException
result = String.Format("Error (CreateTblOfficials - OleDbException): Table creation failed: '{0}'; {1}", tableName, ex.Message)
Catch ex As Exception
result = String.Format("Error (CreateTblOfficials): Table creation failed: '{0}'; {1}", tableName, ex.Message)
End Try
Return result
End Function
Private Function ExecuteNonQuery(sqlText As String) As Integer
Dim rowsAffected As Integer = 0
'used for insert/update
'create new connection
Using cn As OleDbConnection = New OleDbConnection(_connectionStr)
'open
cn.Open()
'create new instance
Using cmd As OleDbCommand = New OleDbCommand(sqlText, cn)
'execute
rowsAffected = cmd.ExecuteNonQuery()
End Using
End Using
Return rowsAffected
End Function
Public Function GetImageAsByteArray(filename As String) As Byte()
'read image from file and return as Byte()
Try
If Not String.IsNullOrEmpty(filename) AndAlso System.IO.File.Exists(filename) Then
Using fs As FileStream = New FileStream(filename, FileMode.Open, FileAccess.Read)
Dim imageBytes(fs.Length) As Byte
'read image from file and put into Byte()
fs.Read(imageBytes, 0, fs.Length)
Return imageBytes
End Using
End If
Catch ex As Exception
Debug.WriteLine("Error (GetImageAsByteArray): " + ex.Message)
Throw
End Try
Return Nothing
End Function
Public Function TblOfficialsExecuteNonQuery(sqlText As String, fullName As String, birthdate As Date, jobDescription As String, term As String, status As String, imageBytes As Byte()) As Integer
Dim rowsAffected As Integer = 0
'create new connection
Using cn As OleDbConnection = New OleDbConnection(_connectionStr)
'open
cn.Open()
'create new instance
Using cmd As OleDbCommand = New OleDbCommand(sqlText, cn)
'OLEDB doesn't use named parameters in SQL. Any names specified will be discarded and replaced with '?'
'However, specifying names in the parameter 'Add' statement can be useful for debugging
'Since OLEDB uses anonymous names, the order which the parameters are added is important
'if a column is referenced more than once in the SQL, then it must be added as a parameter more than once
'parameters must be added in the order that they are specified in the SQL
'if a value is null, the value must be assigned as: DBNull.Value
With cmd.Parameters
.Add("!fullName", OleDbType.VarChar).Value = If(String.IsNullOrEmpty(fullName), DBNull.Value, fullName)
.Add("!birthDate", OleDbType.Date).Value = birthdate
.Add("!jobDescription", OleDbType.VarChar).Value = If(String.IsNullOrEmpty(jobDescription), DBNull.Value, jobDescription)
.Add("!term", OleDbType.VarChar).Value = If(String.IsNullOrEmpty(term), DBNull.Value, term)
.Add("!status", OleDbType.VarChar).Value = If(String.IsNullOrEmpty(status), DBNull.Value, status)
'set size to -1, otherwise it defaults to a maxium of 8000
.Add("!photo", OleDbType.VarBinary, -1).Value = imageBytes
End With
'ToDo: remove the following code that is for debugging
'For Each p As OleDbParameter In cmd.Parameters
'Debug.WriteLine(p.ParameterName & ": " & p.Value.ToString())
'Next
'execute
rowsAffected = cmd.ExecuteNonQuery()
End Using
End Using
Return rowsAffected
End Function
Public Function TblOfficialsGetData() As DataTable
Dim dt As DataTable = New DataTable()
Dim sqlText As String = "SELECT * from Officials"
Try
'create new connection
Using con As OleDbConnection = New OleDbConnection(_connectionStr)
'open
con.Open()
'create new instance
Using cmd As OleDbCommand = New OleDbCommand(sqlText, con)
Using da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
'fill DataTable from database
da.Fill(dt)
End Using
End Using
End Using
Return dt
Catch ex As OleDbException
Debug.WriteLine("Error (TblOfficialsGetData - OleDbException) - " & ex.Message & "(" & sqlText & ")")
Throw ex
Catch ex As Exception
Debug.WriteLine("Error (TblOfficialsGetData) - " & ex.Message & "(" & sqlText & ")")
Throw ex
End Try
End Function
Public Function TblOfficialsInsert(fullName As String, birthdate As Date, jobDescription As String, term As String, status As String, imageBytes As Byte()) As Integer
Dim rowsAffected As Integer = 0
Dim sqlText As String = String.Empty
sqlText = "INSERT INTO Officials ([FullName], [BirthDate], [JobDescription], [Term], [Status], [Photo]) VALUES (?, ?, ?, ?, ?, ?);"
Try
'insert data to database
Return TblOfficialsExecuteNonQuery(sqlText, fullName, birthdate, jobDescription, term, status, imageBytes)
Catch ex As OleDbException
Debug.WriteLine("Error (TblOfficialsInsert - OleDbException) - " & ex.Message & "(" & sqlText & ")")
Throw ex
Catch ex As Exception
Debug.WriteLine("Error (TblOfficialsInsert) - " & ex.Message & "(" & sqlText & ")")
Throw ex
End Try
Return rowsAffected
End Function
Public Function TblOfficialsUpdate(fullName As String, birthdate As Date, jobDescription As String, term As String, status As String, imageBytes As Byte()) As Integer
Dim rowsAffected As Integer = 0
Dim sqlText As String = String.Empty
sqlText = "UPDATE Officials SET [FullName] = ?, [Birthdate] = ? , [JobDescription] = ?, [Term] = ?, [Status] = ?, [Photo] = ?;"
Try
'update data in database
Return TblOfficialsExecuteNonQuery(sqlText, fullName, birthdate, jobDescription, term, status, imageBytes)
Catch ex As OleDbException
Debug.WriteLine("Error (TblOfficialsUpdate - OleDbException) - " & ex.Message & "(" & sqlText & ")")
Throw ex
Catch ex As Exception
Debug.WriteLine("Error (TblOfficialsUpdate) - " & ex.Message & "(" & sqlText & ")")
Throw ex
End Try
Return rowsAffected
End Function
End Class
Usage
Create Access Database:
Private _helper As HelperAccess = Nothing
...
Dim sfd As SaveFileDialog = New SaveFileDialog()
sfd.Filter = "Access Database (*.accdb)|*.accdb|Access Database (*.mdb)|*.mdb"
If sfd.ShowDialog() = DialogResult.OK Then
'create new instance
_helper = New HelperAccess(sfd.FileName)
Dim result As String = _helper.CreateDatabase()
End If
Create Table
Private _helper As HelperAccess = Nothing
...
Dim result As String = _helper.CreateTblOfficials()
Insert data to database:
Private _helper As HelperAccess = Nothing
...
Dim imageBytes As Byte() = Nothing
imageBytes = System.IO.File.ReadAllBytes("C:\Temp\Images\Test1.jpg")
_helper.TblOfficialsInsert("Joe Smith", New Date(1986, 5, 20), "Captain", "2016-2030", "Active", imageBytes)
Get data from database:
Add a DataGridView to your form from the Toolbox (don't add any columns)
Private _dt As DataTable = New DataTable()
Private _helper As HelperAccess = Nothing
Private _source As BindingSource = New BindingSource()
...
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'set properties
DataGridView1.AllowUserToAddRows = False
DataGridView1.AllowUserToDeleteRows = False
'set data source
DataGridView1.DataSource = _source
End Sub
Private Sub GetData()
'get data from database
_dt = _helper.TblOfficialsGetData()
'set value
_source.DataSource = _dt
_source.ResetBindings(True)
End Sub
Resources
CREATE TABLE statement (Microsoft Access SQL)
How can I refresh c# dataGridView after update?
Getting binary data using SqlDataReader
I've got a utility function in a much larger project that updates a backend SQL database. It's currently failing most times I use it, with the error:
There is already an open DataReader associated with this Command which must be closed first.
The code for the function is below:
Public Function Update_Data(what As String, Optional where As String = "",
Optional table As String = ThisAddIn.defaultTable) As Integer
Try
Dim cmd As New SqlCommand With {
.Connection = conn
}
cmd.CommandText = "UPDATE " & table & " SET " & what
If where <> "" Then
cmd.CommandText &= " WHERE " & where
End If
Update_Data = cmd.ExecuteNonQuery
cmd.Dispose()
Catch ex As Exception
Update_Data = 0
Debug.WriteLine("SQL Error updating data:" & vbCrLf & ex.Message)
End Try
End Function
I've gone through the rest of the code to make sure that whenever I have a SQLDataReader declared I later call reader.close(). I added the cmd.Dispose() line to this and all the other ExecuteNonQuery functions I could find - incase that helped?
Are there any other instances/types of reader that might not be being closed?
In the case of an Exception, you aren't disposing your command.
If you don't want to use Using, add a Finally
Public Function Update_Data(what As String, Optional where As String = "",
Optional table As String = ThisAddIn.defaultTable) As Integer
Dim cmd As SqlCommand
Try
cmd = New SqlCommand With {.Connection = conn}
cmd.CommandText = "UPDATE " & table & " SET " & what
If where <> "" Then
cmd.CommandText &= " WHERE " & where
End If
Update_Data = cmd.ExecuteNonQuery
Catch ex As Exception
Update_Data = 0
Debug.WriteLine("SQL Error updating data:" & vbCrLf & ex.Message)
Finally
cmd.Dispose()
End Try
End Function
but Using might be simpler
Public Function Update_Data(what As String, Optional where As String = "",
Optional table As String = ThisAddIn.defaultTable) As Integer
Using cmd As New SqlCommand With {.Connection = conn}
Try
cmd.CommandText = "UPDATE " & table & " SET " & what
If where <> "" Then
cmd.CommandText &= " WHERE " & where
End If
Update_Data = cmd.ExecuteNonQuery
Catch ex As Exception
Update_Data = 0
Debug.WriteLine("SQL Error updating data:" & vbCrLf & ex.Message)
End Try
End Using
End Function
i confused why mySqlDataReader.Read stuck on the last record and doesnt EOF ..
Here's my private function for executeSql :
Private Function executeSQL(ByVal str As String, ByVal connString As String, ByVal returnRecordSet As Boolean) As Object
Dim cmd As Object
Dim objConn As Object
Try
If dbType = 2 Then
cmd = New MySqlCommand
objConn = New MySqlConnection(connString)
Else
cmd = New OleDbCommand
objConn = New OleDbConnection(connString)
End If
'If objConn.State = ConnectionState.Open Then objConn.Close()
objConn.Open()
cmd.Connection = objConn
cmd.CommandType = CommandType.Text
cmd.CommandText = str
If returnRecordSet Then
executeSQL = cmd.ExecuteReader()
executeSQL.Read()
Else
cmd.ExecuteNonQuery()
executeSQL = Nothing
End If
Catch ex As Exception
MsgBox(Err.Description & " #ExecuteSQL", MsgBoxStyle.Critical, "ExecuteSQL")
End Try
End Function
And this is my sub to call it where the error occurs :
Using admsDB As MySqlConnection = New MySqlConnection("server=" & rs("server") & ";uid=" & rs("user") & ";password=" & rs("pwd") & ";port=" & rs("port") & ";database=adms_db;")
admsDB.Open()
connDef.Close()
rs.Close()
'get record on admsdb
Dim logDate As DateTime
Dim str As String
str = "select userid, checktime from adms_db.checkinout in_out where userid not in (select userid " &
"from adms_db.checkinout in_out join (select str_to_date(datetime,'%d/%m/%Y %H:%i:%s') tgl, fid from zsoft_bkd_padang.ta_log) ta " &
"on ta.fid=userid and tgl=checktime)"
Dim rsAdms As MySqlDataReader = executeSQL(str, admsDB.ConnectionString, True)
Dim i As Integer
'This is where the error is, datareader stuck on the last record and doesnt EOF
While rsAdms.HasRows
'i = i + 1
logDate = rsAdms(1)
'save to ta_log
str = "insert into ta_log (fid, Tanggal_Log, jam_Log, Datetime) values ('" & rsAdms(0) & "','" & Format(logDate.Date, "dd/MM/yyyy") & "', '" & logDate.ToString("hh:mm:ss") & "', '" & logDate & "')"
executeSQL(str, oConn.ConnectionString, False)
rsAdms.Read()
End While
'del record on admsdb
str = "truncate table checkinout"
executeSQL(str, admsDB.ConnectionString, False)
End Using
i'm new to vbnet and really have a little knowledge about it,, please help me,, and thank you in advance..
The issue is that you're using the HasRows property as your loop termination expression. The value of that property never changes. Either the reader has rows or it doesn't. It's not a check of whether it has rows left to read, so reading has no effect.
You are supposed to use the Read method as your flag. The data reader begins without a row loaded. Each time you call Read, it will load the next row and return True or, if there are no more rows to read, it returns False.
You normally only use HasRows if you want to do something special when the result set is empty, e.g.
If myDataReader.HasRows Then
'...
Else
MessageBox.Show("No matches found")
End If
If you don't want to treat an empty result set as a special case then simply call Read:
While myDataReader.Read()
Dim firstFieldValue = myDataReader(0)
'...
End While
Note that trying to access any data before calling Read will throw an exception.
I've successfully taught myself how to pull some data from a MySQL database and bind it to a ListView control (ListViewCard).
Now I can't figure out how to use the SelectedIndexChanged event to inerate through the records and populate some other controls on my form (i.e, 7 textboxes, 2 comboboxes, and 2 datetimepickers).
Your help would be greatly appreciated. Here's my code:
Private Sub loadCard()
Try
'FOR MySQL DATABASE USE
'Dim dbQuery As String = ""
'Dim dbCmd As New MySqlCommand
'Dim dbAdapter As New MySqlDataAdapter
Dim dbTable As New DataTable
Dim i As Integer
If dbConn.State = ConnectionState.Closed Then
dbConn.ConnectionString = String.Format("Server={0};Port={1};Uid={2};Password={3};Database=accounting", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password)
dbConn.Open()
End If
dbQuery = "SELECT *" & _
"FROM cc_master INNER JOIN customer ON customer.accountNumber = cc_master.customer_accountNumber " & _
"WHERE customer.accountNumber = '" & TextBoxAccount.Text & "'"
With dbCmd
.CommandText = dbQuery
.Connection = dbConn
End With
With dbAdapter
.SelectCommand = dbCmd
.Fill(dbTable)
End With
ListViewCard.Items.Clear()
For i = 0 To dbTable.Rows.Count - 1
With ListViewCard
.Items.Add(dbTable.Rows(i)("ccID"))
With .Items(.Items.Count - 1).SubItems
.Add(dbTable.Rows(i)("ccNumber"))
.Add(dbTable.Rows(i)("ccExpireMonth"))
.Add(dbTable.Rows(i)("ccExpireYear"))
End With
End With
Next
Catch ex As MySqlException
MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
End Try
dbConn.Close()
End Sub
I'm using the following function to generate a list of users connected to a selected database. How would I change this to a single line for multiple identical results?
For example: "sa (3) - MYCOMPUTER" rather than listing "sa - MYCOMPUTER" three times?
Function ConnectedUsers(ByVal SelectedDatabase As String, ByVal SelectedInstance As String)
Dim myCommand As SqlCommand
Dim dr As SqlDataReader
Dim mystring As String = String.Empty
Try
Dim myConn As New SqlConnection(ConnectionString)
myConn.Open()
myCommand = New SqlCommand("select loginame,hostname from sysprocesses where db_name(dbid) = '" & SelectedDatabase & ";", myConn)
dr = myCommand.ExecuteReader()
While dr.Read()
mystring += GetFullName(dr(0).ToString().Trim()) & " - " & dr(1).Trim() & vbCrLf
End While
dr.Close()
myConn.Close()
Catch e As Exception
MessageBox.Show(e.Message)
End Try
Return mystring
End Function
Thanks.
The SQL Command should be
select loginame, count(*) as Nbr, hostname from sysprocesses where db_name(dbid) = '" & SelectedDatabase & "' group by loginame;"
and you should change the display to show the count (Nbr in this example) to be something like:
mystring += GetFullName(dr(0).ToString().Trim()) & "(" & dr(1).Trim() & ") - " & dr(2).Trim() & vbCrLf