4

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
marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Mar 26, 2014 at 7:13
13
  • 2
    Is Table the name of your table? It is an SQL keyword, how did you use that name for your table? Commented Mar 26, 2014 at 7:16
  • 1
    Sorry, I copied a bad version of the code. The name of the table is Tabela, and in the code is tabela too. I edited the code. thanks for noticing it Commented Mar 26, 2014 at 7:20
  • The message box is shown? Commented Mar 26, 2014 at 7:21
  • Yes, the message it's shown Commented Mar 26, 2014 at 7:23
  • 1
    Sometimes |DataDirectory| as path is problematic when debugging. Did you check the db copy at \bin\debug? Commented Mar 26, 2014 at 8:30

1 Answer 1

2

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"

answered Mar 27, 2014 at 8:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.