Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am coding for a form which will send the value of the textboxes to the sql local database that i created. but an error occured (runtime) in the connection.open() part. What is the problem with my code:

VB
Imports System.Data.SqlClient

Public Class Form1
    Dim Connection As New SqlConnection("Data Source=C:\Users\user\AppData\Local\Temporary Projects\WindowsApplication1\Example.sdf")
    Dim Command As New SqlCommand
    Dim DataReader As SqlDataReader

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Command.Connection = Connection
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Connection.Open()
        Command.CommandText = "INSERT INTO ExampleTable (FirstName, LastName) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "')"
        Command.ExecuteNonQuery()
        Connection.Close()
    End Sub
End Class



Posted
Comments
Ron Beyer 11-May-13 15:21pm    
What kind of database is it? Take a look at this site (http://www.connectionstrings.com/) for proper connection strings, but SqlConnection only connects to MS SQL databases.
[no name] 11-May-13 15:48pm    
The error message would be a big clue as to what your problem is....
RedDk 11-May-13 17:21pm    
An .sdf file ... ok. Here's what I'd do. Open THAT file in smsse just to make sure THAT'LL happen. Then work from the VBNET app side and see if its possible to create a connection string that'll match the instance just tapped manually.

Pay particular attention to how ssmse displays info about that new database. Much different than an .mdf.

1 solution

You don't open SDF files using SQL server - you use SqlCeConnection and SqlCeCommand objects instead:
VB
Using con As New SqlCeConnection("Data Source=C:\Users\griff\Documents\AA Backed Up\My Databases\StandardReplies.sdf")
	Using cmd As New SqlCeCommand("SELECT * FROM Replies", con)
                ...
	End Using
End Using
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900