Question No new row insertion and no error message!!

The

Member
Joined
Nov 27, 2014
Messages
16
Programming Experience
3-5
Here is my code.
I have access to the db from Vb.net, the column get the data from it, but when I insert new row to the table, nothing happen. No error message at all, and the new record doesn't appear in my db.
I am still learning here, and I would appreciate any help.
VB.NET:
    Private Function Db_Connect() As Boolean

        If sqlconn.State = ConnectionState.Closed Then
            sqlconn.ConnectionString = connString
            sqlconn.Open()
            MsgBox("Connected!")
        End If
        Return Nothing

    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If Me.ValidateChildren() Then
            Try
                Db_Connect()
                sqlquery.CommandText = "Insert into Units ( [B_ID], [F_ID], [U_Number], [Rooms], [Current_Tenant], [Default_Rent], [Current_Rent] ) Values ( " & CStr(Buildings_List.SelectedValue) & ", " & CStr(Floors_List.SelectedValue) & ", '" & Unit_Name.Text & "', '" & Rooms.Text & "', 19, " & Rent.Text & ", 0)"
                sqlquery.Connection = sqlconn
                'Dim msg As String = sqlquery.ExecuteNonQuery()
                Try
                    sqlquery.ExecuteNonQuery()

                Catch ex As Exception
                    MessageBox.Show(ex.Message & " - " & ex.Source)
                End Try

            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
     End If
End Sub

Thanks
 
I fixed that by choosing "Do Not Copy" for the option "Copy to output directory" under the db properties

But I faced another problem. I used to use: |datadirectory|\Data.accdb

Now I am using: C:\Users\Dani\Desktop\Sol\Sol_VB\Pro\Data.accdb

Is there any better way to set the location of the db to be flixible for the debug comiler and to make it ready for distribution?
 
Here's what happens with local data files. VS cannot create Access data files so presumably you created it in Access. You should then add the MDB or ACCDB file to your project. You would be prompted to copy the file to the project folder and you should accept. You should then simply ignore the original file. The copy created is now part of your project and considered to be the source file. If you ever need to make changes to the source file, e.g. change the schema or add or modify default data, you should modify the copy in your project.

You should then set the Copy To Output Directory property of that project item to Copy If Newer. What that means is that, when you build your project, the source data file will be copied from the project folder to the output folder if and only there is no data file already in the output folder or the last modified time of the source file is later than the copy in the output folder. As a result, a copy of the data file will be made in the output folder when you first build and that copy will not be overwritten unless you actually make a change to the source file.

That is what most people want because it means that they can make changes to the copy while debugging, make changes to their code and then debug again without losing the changes they made last time. If you use Copy Always, which is the default, then every time you make changes to your code and build, you will overwrite the output copy that contains your test data with a new clean copy of the source file. If you use Copy Never then you'll never see a data file in your output folder created automatically and it's up to you to manually place the data file where you want it, which may be the output folder or elsewhere.

If you don't copy the original data file into your project in the first place and test against the one and only data file then you will be polluting your one and only data file with test data each time you debug, so you'd have to waste time and effort to clean it up when it comes time to deploy. Do what I said in the first two paragraphs and it will just work. It's what I always do with local data files and many thousands of other developers do the same.
 
Note that what this also means is that, when it comes time to deploy, you can create a Release build and the current source data file, which is clean and correct, will be copied to the Release output folder and you can just deploy it as is without having to do any extra work. If you follow the instructions I provided then you should go back to use |DataDirectory| in your connection string, which is why it's the default. The problem is, it sounds like you may have already polluted your original data file. If so then I'd suggest creating a new one and keeping it clean.
 
Back
Top