# Solved: Visual Basic 2010 Express - SQL Database



## SR2012 (Jun 10, 2012)

Hi,

I am using VB.Net. I am trying to pull a value from a database and pasting it to a text box. This is what I have so far. It is not working. For the database, I am using the SQL Server Express Compact version.

Dim Var
Var = ID.Text

Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
 
dbProvider = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;"
dbSource = "Data Source=C:\Users\Main\Desktop\VB\Database.sdf;"
con.ConnectionString = dbProvider & dbSource
con.Open()

sql = "SELECT Name FROM Test where ID = ' " & Var & " '"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Name")
TextBox1.Text = ds.Tables("Name")
 
con.Close()
 

Thanks


----------



## ckphilli (Apr 29, 2006)

I'd check the connection string first. You can do this by inserting breakpoints or adding a line in your code to write the db output to the console.


----------



## ckphilli (Apr 29, 2006)

By the way...believe you need spaces after "as" .


----------



## SR2012 (Jun 10, 2012)

I have the spaces after "as" in the code.


----------



## ckphilli (Apr 29, 2006)

Good. So did you test the connection string?


----------



## SR2012 (Jun 10, 2012)

The below worked.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Var
Var = ID.Text
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
dbProvider = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;"
dbSource = "Data Source=C:\Users\Main\Desktop\VB\Database.sdf;"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT Name FROM Test where ID = '" & Var & "'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "DataLoad")
TextBox1.Text = ds.Tables("DataLoad").Rows(0).Item(0)
con.Close()
End Sub


----------



## ckphilli (Apr 29, 2006)

Cool, can you mark the thread as solved please?


----------

