-
[RESOLVED] Received error when I click button
I need your help, I am trying to get over this problem and I don't know why I get an error message everytime when I click the button.
Here it the code:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * From Table1", "Put an actual connection string here. THIS SHOULD NOT BE COPIED MINDLESSLY AND EXPECTED TO WORK")
Dim dt As New DataTable
da.Fill(dt)
Dim img As Image
For Each row As DataRow In dt.Rows
img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("NameOfImageColumn").Value, Byte())))
'Do something with the image
Next
End Sub
Error: Format of the initialization string does not conform to specification starting at index 0.
It shows highlight on Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * From Table1", "Put an actual connection string here. THIS SHOULD NOT BE COPIED MINDLESSLY AND EXPECTED TO WORK") as statement. What can I do to get this fix??
Thanks,
Mark
-
Re: Received error when I click button
You are kidding me, right? Did you actually read that code? Look at the second argument you're passing to your OleDbDataAdapter constructor:
Quote:
Put an actual connection string here. THIS SHOULD NOT BE COPIED MINDLESSLY AND EXPECTED TO WORK
Despite the fact that that explicitly tells you to put an actual connection string there, you seem to have mindlessly copied it and expected it to work.
-
Re: Received error when I click button
Thanks for your reply JMC. I am not really kidding to you, what shall I do with that second argument you have shown on your post??
Shall I input the name of the columns from my database??
Thanks,
Mark
-
Re: Received error when I click button
No, you shall do what it specifically tells you to do:
Quote:
Put an actual connection string here.
You can get the connection string format for your data source at www.connectionstrings.com. Note that it will give you the format only. It's still up to you to put the correct parameter values into it.
-
Re: Received error when I click button
Ok thanks, I am using microsoft access 2007 so why did you post the link to find out the connection string format for my data source?? It won't tell me on that site where I have take a look so hope we can get this resolve if you please tell me an instructions what I do with??
The link you post gave me no where to go.
Thanks,
Mark
-
Re: Received error when I click button
Code:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
Is that connection string format for my data source where I should replace Put an actual connection string here. on my statement??
Thanks,
Mark
-
Re: Received error when I click button
Quote:
Originally Posted by Mark107
Code:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
Is that connection string format for my data source where I should replace
Put an actual connection string here. on my statement??
Thanks,
Mark
Did you try it? Did it work? If it did then the answer is "yes", otherwise the answer is "no".
-
Re: Received error when I click button
LOL, i'm the one who gave him the original code. I thought i'd made it pretty clear that it shouldn't be copied and pasted, but oh well.
Anyway, Mark, how about you answer a few questions instead:
Is this a multi-user program?
If your answer was YES, then is the database file being stored on a network PC?
If your answer was NO, then we will assume that it will be located in Application.StartupPath, and probably without any kind of security, in which case, you could try this:
Code:
Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * From Table1", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\myAccess2007file.accdb;Persist Security Info=False;")
-
Re: Received error when I click button
Yes I did and it's nearly fix but there is another error message, The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
It show the yellow highlight on da.Fill(dt) statement. I don't see what's the problem is??
Thanks,
Mark
-
Re: Received error when I click button
Quote:
Originally Posted by Mark107
Yes I did and it's nearly fix but there is another error message, The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
It show the yellow highlight on da.Fill(dt) statement. I don't see what's the problem is??
Thanks,
Mark
Do you have Access 2007 installed on your machine? If not then you will have to install the Office 2007 driver pack, downloadable from Microsoft, to install the Access 2007 OLEDB provider.
-
Re: Received error when I click button
I only have microsoft access 2002 on my machine. Can you post a link on here because i took a look on google and none of them have found??
Thanks,
Mark
Quote:
Originally Posted by jmcilhinney
Do you have Access 2007 installed on your machine? If not then you will have to install the Office 2007 driver pack, downloadable from Microsoft, to install the Access 2007 OLEDB provider.
-
Re: Received error when I click button
I have rename from Provider=Microsoft.ACE.OLEDB.12.0;Data Source= to Provider=Microsoft.Jet.OLEDB.4.0;Data source= so when I run the form and when I click the button, it highlighting on da.Fill(dt) as statement with the error OleDBExpection was unhandled Not a valid file name.
It do not means I am using old version but something is incorrect and need to fix??
Thanks,
Mark
-
Re: Received error when I click button
Hi Mark,
The database which you want to connect to, is Access 2007. Your local computer only has the neccessary files to talk to Access versions upto and including 2003. Therefore, in order for your local machine and indeed your code, to communicate with your database, you will need to install or upgrade the office libraries/certain Office files upon your computer.
One way of doing this would be to upgrade your local copy of Office 2002 to an Office 2007 version, another way would be to install Office 2007 onto your computer alongside your existing Office 2002 version and the third and perhaps easiest option would be to install the Office 2007 driver pack which has already been suggested above.
As for your connection string and related to your last question/post there, I would suggest you research what a connection string is to begin with. First off, this is direct from MS:
Quote:
A connection string is a string version of the initialization properties needed to connect to a data store and enables you to easily store connection information within your application or to pass it between applications. Without a connection string, you would be required to store or pass a complex array of structures to access data.
Basically, to make a connection to a database, several items of information are required to be known. The first is the type of the database, the second is the location of the database, then you may have valid login or authorisation information for this specific database . These are the basic items of information required before VB will make the database connection. A connection string is a ";" delimited string representation of these items of information.
Once you grasp this part, the connectionstrings site, and indeed the individual ; separated parts of the connectionstring examples shown upon that site should fall slowly into place. To further explain your last post, the provider connectionstring specifier details the type of database you are wanting to connect to. Using the following, once you've set your machine up to talk to Office 2007 applications, will allow you to connect specifically to an Office 2007 Access database:
Code:
Provider=Microsoft.ACE.OLEDB.12.0;
Next up, the Data Source specifier details the location of your database, so you need to add this in after your equals sign, like this:
Code:
Data Source=C:\myFolder\myAccess2007file.accdb
It looks as though the connection string in your post 6 may well work if that database location is right and you have your machine setup to communicate with Office 2007 applications properly.
-
Re: Received error when I click button
Thanks for your alex_read, unfornately there is no package for win 2000 where I am using on. However I find other useful code which let me reading information from the database.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * From Table1", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db1_test.mdb;Persist Security Info=False;")
Dim dt As New DataTable
da.Fill(dt)
Dim img As Image
For Each row As DataRow In dt.Rows
img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images").Value, Byte())))
'Do something with the image
Next
End Sub
When I run the form and when I click the button, I have an error message saying Public member 'Value' on type 'Byte()' not found. It show the highlight on img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images").Value, Byte()))) as statement.
Please can you help to get this fix??
Thanks,
Mark
-
Re: Received error when I click button
Are you developing the app on what system and office verison? Is the db located on a different 2k system?
-
Re: Received error when I click button
The office version is 2003 and the os is win 2000 so I prefer to use Microsoft.Jet.OLEDB.4.0;Data Source which it will works. However, when I run the form and when I click the button, I have an error message saying Public member 'Value' on type 'Byte()' not found. It show the highlight on img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images").Value, Byte()))) as statement.
Please can you help to get this fix??
Thanks,
Mark
-
Re: Received error when I click button
Now I'm confused as above you put "Ok thanks, I am using microsoft access 2007" :eek: :confused: :confused:
ANyhow with your last point there, could your datatable possibly contain NULL values / is it possible one of your rows does not have an entry in the images column at all?
-
Re: Received error when I click button
Are you using a *.accdb Access database file or an *.mdb Access database file?
-
Re: Received error when I click button
Sorry I typed by my mistake. I should have input "Ok thanks, I am using microsoft access 2002" in the first place. Anyway, I believe they could work in both ways and I think the rows do have entry in the images column but the images do not shown on my listview.
Hope there is a way to get it resolve.
Thanks,
Mark
Quote:
Originally Posted by alex_read
Now I'm confused as above you put "Ok thanks, I am using microsoft access 2007" :eek: :confused: :confused:
ANyhow with your last point there, could your datatable possibly contain NULL values / is it possible one of your rows does not have an entry in the images column at all?
-
Re: Received error when I click button
I am using .mdb access database file.
Quote:
Originally Posted by RobDog888
Are you using a *.accdb Access database file or an *.mdb Access database file?
-
Re: Received error when I click button
Ok then the connectionstring shouldnt be the issue here. The line of code ...
Code:
img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images").Value, Byte())))
Is where you are generating your error so that shold be looked at for correcting.
So do you have "row.Item("Images").Value" actually show up in the intellisense? Is that the correct type to be casting the image data to?
-
Re: Received error when I click button
Then the Jet.OleDB.4.0 is the right connection provider to use definitely.
Can you try with this code please? Does this get rid of that error?
Code:
For Each row As DataRow In dt.Rows
If not (row.Item("Images") is nothing) then
img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images").Value, Byte())))
'Do something with the image
End If
Next
-
Re: Received error when I click button
It is still the same error I have got, it pointing on img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images").Value, Byte()))) said that MissingMemberExpection was Unhandled Public member 'Value' on type 'Byte()' not found.
I have already input the name of the column which it is Images into my database.
Something is not right that going into the same way round??
Thanks,
Mark
-
Re: Received error when I click button
I had this thread open way too long in a browser window so didn't see Rob's reply there. Ignore my post and follow his suggestion. The removal of .Value should sort your issue here.
-
Re: Received error when I click button
I think this should work
Code:
img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images"), Byte())))
I only tested it for valid syntax but didnt mock up a populated dt.
-
Re: Received error when I click button
Thanks for your advise, I have take a test with my form and when I click the button, there is no images and text data to displaying on my listview.
http://img176.imageshack.us/img176/4246/imageuf1.jpg
There is nothing on my listview as it is blank. What's going on, something is not right?? :confused:
Thanks,
Mark
-
Re: Received error when I click button
Step through your code to make sure you are returning records.
-
Re: Received error when I click button
Code:
Public Class Form1
Dim xConn As sqlConn
Public Shared item1 As New ListViewItem()
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETEXTENDEDLISTVIEWSTYLE As Long = (LVM_FIRST + 54)
Private Const LVS_EX_FLATSB As Long = &H100
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * From Table1", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db1_test.mdb;Persist Security Info=False;")
Dim dt As New DataTable
da.Fill(dt)
Dim img As Image
For Each row As DataRow In dt.Rows
img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images").Value, Byte())))
'Do something with the image
Next
End Sub
End Class
Code:
Public Class sqlConn
Friend WithEvents OLEConn As New System.Data.OleDb.OleDbConnection()
Friend WithEvents OLEComm As New System.Data.OleDb.OleDbCommand()
Private sqlString As String
Private err As System.Exception
Public Shared dataReturned As New ArrayList()
Public Property db() As String
Get
db = "C:\\db1_test.mdb"
End Get
Set(ByVal Value As String)
Value = db
End Set
End Property
Public Property xOLE() As String
Get
xOLE = "Provider=Microsoft.Jet.OLEDB.4.0;Data source="
End Get
Set(ByVal Value As String)
Value = xOLE
End Set
End Property
Function connectMe(ByVal sqlString As String) As Boolean
Try
OLEConn.ConnectionString = xOLE & db
OLEConn.Open()
OLEComm.CommandText = sqlString
Return True
Catch err As System.Exception
MsgBox(err.Message)
Return False
End Try
End Function
Function getData() As ArrayList
OLEComm.Connection = OLEConn
getData = New ArrayList()
Dim d As OleDb.OleDbDataReader = OLEComm.ExecuteReader()
Do While d.Read
getData.Add(d("Images".ToString))
getData.Add(d("Cars".ToString))
Loop
'Returns array collection
dataReturned = getData
End Function
End Class
-
Re: Received error when I click button
do you know what the problem is and how to fix it??
Thanks,
Mark
-
Re: Received error when I click button
Your sqlConn class is terrible. Do some research on Properties and how they should be coded. Just so you know, Get should return the variable, Set should assign it a value.
-
Re: Received error when I click button
Here is how your db property should look. (You are currently hard coding in a value so the set will never change it)
Code:
Private m_db As String
Public Property db() As String
Get
Return m_db
End Get
Set(ByVal value As String)
m_db = value
End Set
End Property
-
Re: Received error when I click button
Thanks for your input RobDog, I have replaced the code you post on here but it is still the same where it was, no image to display on my listview.
I don't know what I should do now, maybe I should post attachment then??
Thanks,
Mark
-
Re: Received error when I click button
The property code was just an extra fix that may have helped with changing your connection to the db but ...
If you want to attach your solution and database in zip format I'm sure it would be easier to test out your situation. Ps, remove the debug and Release folders to help reduce the size.
-
1 Attachment(s)
Re: Received error when I click button
There you go! Hope you can be able to situations and get the problem fixed asap!
Thanks,
Mark
-
Re: Received error when I click button
do anyone know how to fix this????????????????
-
Re: Received error when I click button
That's because you're never calling any code to output your database values to your listbox, all you have is a comment similar to "do something with the values here".
This code will work providing you change where I have put "C:\Path" to the actual directory location your database is found within. Also don't use double \\ characters for this path. That is not needed.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Connect to the database and retreive the data required from Table1 into a datatable object.
Dim da As New System.Data.OleDb.OleDbDataAdapter( _
"SELECT Car, Images FROM Table1 ORDER BY Car DESC", _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Path\db1_test.mdb;Persist Security Info=False;")
Dim dt As New DataTable
da.Fill(dt)
' Place a new imagelist control onto your form designer window. The following
' will assign the listview to read from this control.
With listview1
.LargeImageList = imagelist1
.SmallImageList = imagelist1
End With
Dim img As Image
Dim intImageIndex as Integer = 0
' Loop through each row in the database.
For Each row As DataRow In dt.Rows
' First off, check we have a record value within the Car column.
If not string.IsNullOrEmpty(row.Item("Car")) Then
' If there is an image contained within the row, retreive this into a new image object.
' Next, add this image into the imagelist control & output the car name and image to the
' listview as a newlist item. This last call references the image by index number, which
' is kept track of in the intImageIndex variable - incremented upon each loop iteration.
If Not(row.Item("Images") is nothing) then
img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images"), Byte())))
imagelist1.Images.Add(row.Item("Car").ToString, img)
listview1.Items.Add(row.Item("Car").ToString, intImageIndex)
Else
' If we arrived at this point, we have a car record entry, but no image. Therefore
' we will just output the info we do have into a new listview listitem.
listview1.Items.Add(row.Item("Car").ToString)
End If
Else
' If we arrived here at this point in the code, then no record beneath the Car column
' exists for the row. Here we check if there's an image record and output that if
' existing in the same manner as above.
If Not(row.Item("Images") is nothing) then
img = Image.FromStream(New IO.MemoryStream(DirectCast(row.Item("Images"), Byte())))
imagelist1.Images.Add("{NULL Value}", img)
listview1.Items.Add("{NULL Value}", intImageIndex)
End If
End If
intImageIndex +=1
Next
If Me.ListView1.Items.Count >= 12 Then
Me.VScrollBar1.Enabled = True
ListView1.Scrollable = True ' I've altered this, I believe you want true here.
VScrollBar1.Maximum = ListView1.Items.Count
End If
End Sub
-
Re: Received error when I click button
-
Re: Received error when I click button
Thanks for your help alex_read, you have been awesome. We have finally found the situations to get it sorting so now it's resolve ;)
Thanks for the help, keep up the good work! :thumb:
Thanks,
Mark