Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,542 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,814 people online right now. Registration is fast and FREE... Join Now!




Retrieve value from table

 
Reply to this topicStart new topic

Retrieve value from table

Damage
22 Aug, 2008 - 04:45 PM
Post #1

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
Sorry, i'm still trying to get to grips with this. What am I doing wrong here?

CODE

  Private Function getimage(ByVal imageID As Integer) As String
        Dim path As String
        Dim pictureID As Integer = ComboBox1.Text
        Dim sqlString As String = ("SELECT Picture_Path FROM tblPicture WHERE Product_ID = '" & pictureID & "' ")

        ' Open Connection
        Dim databasePath As String = "C:\PictureDB\PictureDB.mdb"
        Dim cnJetDB As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + databasePath)
        cnJetDB.Open()


        Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(sqlString, cnJetDB)

        Dim picture As DataSet = New DataSet
        adapter.Fill(picture, "Pictures")
        path = picture.Tables.Item(0).ToString

        Return (path)
    End Function


the error I'm getting is "Data type mismatch in criteria expression"

I just want to retrieve the text stored at that particular record, it's an autonumber so there's no duplicate values or anything
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Retrieve Value From Table
22 Aug, 2008 - 05:05 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Chances are, it's the single quotes. They should only go around strings. So,
CODE

Dim sqlString As String = "SELECT Picture_Path FROM tblPicture WHERE Product_ID = " & pictureID


Also, if you're using a DataAdapter, you should worry about opening a connection. Consider putting the connection in it's own method as well. e.g.
CODE

Private Function GetNewConnection() As OleDbConnection
    Dim databasePath As String = "C:\PictureDB\PictureDB.mdb"
    Return New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + databasePath)
End Function

Private Function GetImage(ByVal imageID As Integer, ByVal pictureID As Integer) As String
    Dim path As String
    Dim sqlString As String = "SELECT Picture_Path FROM tblPicture WHERE Product_ID = " & pictureID

    Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(sqlString, GetNewConnection())

    Dim picture As DataSet = New DataSet
    adapter.Fill(picture, "Pictures")
    Return picture.Tables.Item(0).ToString
End Function

Private Function GetImage(ByVal imageID As Integer) As String
    Dim pictureID As Integer = ComboBox1.Text
    Return GetImage(imageID, pictureID)
End Function


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Damage
RE: Retrieve Value From Table
22 Aug, 2008 - 05:46 PM
Post #3

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
thanks alot, but i'm still doing something wrong.
CODE

        Dim path As String
        Dim sqlString As String = "SELECT tblPicture.Picture_Path FROM tblPicture WHERE (((tblPicture.Product_ID)= " & pictureID & "))"

        Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(sqlString, GetNewConnection())

        Dim picture As DataSet = New DataSet
        adapter.Fill(picture)
        path = picture.Tables.Item(0).ToString
        Return path


it returns either "Pictures" or if i remove that from the code(like above) it returns "Table"
The sqlstring works in access and returns the right thing, so i don't think it's that

This post has been edited by Damage: 22 Aug, 2008 - 05:47 PM
User is offlineProfile CardPM
+Quote Post

Damage
RE: Retrieve Value From Table
23 Aug, 2008 - 02:05 AM
Post #4

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
Changed it to this
CODE

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydb\PictureDB1.mdb")
        Dim command As New OleDbCommand("SELECT tblPicture.Picture_Path FROM tblPicture WHERE (((tblPicture.Product_ID)= " & pictureID & "))", conn)
        conn.Open()
        Dim contactreader As OleDbDataReader = command.ExecuteReader
        contactreader.Read()


        path = contactreader("Picture_Path").ToString


For some reason my db wasn't storing the product id even though i was saving it, so i saved it under a new name and it stuck, so it's working fine
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:38PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month