[RESOLVED] Not able to retrieve items from database
I'm using the SQL Server Compact Database and I'm trying to retrieve two columns of data and import that information into a ListView. This is my first attempt at using a database so I looked at JMC's code example, but I'm afraid I'm not getting it.
Here's the code I'm using:
VB.NET Code:
Using connection As New SqlConnection("Data Source=PathToDatabase.sdf;")
Using command As New SqlCommand("SELECT Column1, Column2 FROM Table", connection)
connection.Open()
Using reader As SqlDataReader = command.ExecuteReader
While reader.Read
Dim lvi As New ListViewItem
lvi.Text = CStr(reader("Column1"))
lvi.SubItems.Add(CStr(reader("Column2")))
lvwPokeSelect.Items.Add(lvi)
End While
End Using
End Using
End Using
I think there's something wrong with the command. Of all the information I looked up, none of it completely explained how those items were supposed to be entered, so I took form them what I could.
Any information is appreciated.
Thanks
Re: Not able to retrieve items from database
What behaivior or exception messages are you getting?
I have not used the SQL CE version, so I can't be sure if there are differences. However, it looks like your connection string is odd, unless that is one way the CE version differs from SQL Server.
Does SQL CE point to a file like that?
If so, then you would need to let us know what the actual problem is, because your code looks correct.
Re: Not able to retrieve items from database
The SqlClient namespace that your using is only for SQL Server and SQL Server Express. For SQL Server CE you must use the SqlServerCe namespace, e.g. SqlCeConnection instead of SqlConnection, etc.
Re: Not able to retrieve items from database
I changed the Namespace to SqlServerCe but I'm still not able to retrieve the information.
I got the connection string example from here.
I modified the code like so:
vb Code:
Using connection As New SqlCeConnection("Data Source=PathToDatabase.sdf;")
Using command As New SqlCeCommand("SELECT Column1, Column2 FROM Table", connection)
connection.Open()
Using reader As SqlCeDataReader = command.ExecuteReader
While reader.Read
Dim lvi As New ListViewItem
lvi.Text = CStr(reader("Column1"))
lvi.SubItems.Add(CStr(reader("Column2")))
lvwPokeSelect.Items.Add(lvi)
End While
End Using
End Using
End Using
Before, I received no error messages and now I receive none when opening the app. However, I receive a DllNotFoundException was unhandled error when the app closes.
The code I've posted is all of the code I've added to the application.
Re: Not able to retrieve items from database
Wrap the code in an exception handler and then step through it. Do you make it through? Do you have SQL Server CE installed on the system you're running the app on? If not then you'd need to deploy the SQL Server CE libraries with your app.
Re: Not able to retrieve items from database
I installed the Sql CE Database.
I fixed my problem though. I stepped through the code like you mentioned, JMC, and I noticed that my "Column 1" and "Column 2" were actually both "Column 1".
Thanks again.