Reputation: 1012
I have a weird problem: I'm trying to do some basic procedure to insert data in a database and I don't know what it's happening, but it does not work.
The thing is that I have a database called prova3
that is a SQL Server 2012 database created with vb.net 2013 with a table called tabela
. I created a dataset to see the connection string that is stored in app.config
. The string is the same below. I'm not getting an error, but the data is not inserted. When I go to the Server explorer to see the data in the table, it's empty.
I think the data it's been saved in elsewhere, but I don't know how to fix it, because I think I'm coding this right. This is for vb.net, but I did the same code for asp.net and it works. Weird.
Could you help me?
In the form it's only a textbox1 and a button1 controls. There is no more code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ImageUrlSt As String
Dim command1 As New SqlCommand
Dim con As New SqlConnection
ImageUrlSt = TextBox1.Text
Try
con.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Prova3.mdf;Integrated Security=True"
con.Open()
command1.Connection = con
command1.CommandText = "INSERT INTO Tabela (imageurl) VALUES (@imageurlst)"
command1.Parameters.Add(New SqlParameter("@imageurlst", ImageUrlSt))
command1.ExecuteNonQuery()
MsgBox("News Saved Succesfully")
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
Upvotes: 4
Views: 5735
Reputation: 4512
Sometimes |DataDirectory|
as path is problematic when debugging. Did you check the db copy at \bin\debug
?
There’s a property Copy to Output Directory
and the default value is Copy if newer
(if you’re using .mdf or .mdb file, the default value is Copy always
). You could check this MSDN document to learn what this property means. In short, the local database file will be copied to Output directory, and THAT database is the one that will get updated.
If you don’t want Visual Studio to copy the database file for you, you could set the Copy to Output Directory
property to Do not copy
. Then it’s your choice when and how to overwrite the database file. Of course, you still need two copies of database file: at design time, you’re using the database file in solution directory, while at run time, you’re modifying the one in output directory.
Another option is to use an absolute path at ConnectionString like
con.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\MyProjectFolder\Prova3.mdf;Integrated Security=True"
Upvotes: 2