I keep getting error on my WinForms application whenever I create new database and insert data.
This is how I create the database
Public Function CreateDatabase() As String
Dim ds As String = "Data Source=" + (My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\" + (Application.ProductName) + "\" + (DBName) + ".wv" + ";Persist Security Info=True;"
Dim Conn As New SqlCeConnection(ds)
Dim Cmd1 As New SqlCeCommand()
Dim Cmd2 As New SqlCeCommand()
Dim SqlCeDB As New SqlCeEngine(ds)
Dim Msg As String
Try
If Not System.IO.File.Exists(DBName) Then
Msg = "Database successfully created in " & Application.StartupPath
SqlCeDB.CreateDatabase()
Cmd1.Connection = Conn
Conn.Open()
'Create acPassword Table
Cmd1.CommandText = "CREATE TABLE [wvAccount](" & _
"[acName] [nvarchar](100) NOT NULL, " & _
"[acPassword] [nvarchar](100) NOT NULL," & _
"[acKeyFile] [nvarchar](64) NULL, " & _
"[acLastLogin] [nvarchar](100) NULL, " & _
"[acLastModified] [nvarchar](100) NULL)"
Cmd1.ExecuteNonQuery()
'Create pmPassword Table
Cmd2.Connection = Conn
Cmd2.CommandText = "CREATE TABLE [wvPassword](" & _
"[pmID] [int] CONSTRAINT pmID PRIMARY KEY NOT NULL, " & _
"[pmCat] [nvarchar](100) NOT NULL, " & _
"[pmTitle] [nvarchar](100) NOT NULL," & _
"[pmUsername] [nvarchar](100) NOT NULL," & _
"[pmSiteURL] [nvarchar](100) NULL," & _
"[pmNotes] [nvarchar](300) NULL)"
Cmd2.ExecuteNonQuery()
Else
Msg = "There is already a Database with that name."
End If
Catch ex As Exception
Msg = ex.Message
End Try
Return Msg
End Function
And this is how I insert the data right after I create the database and populate with table
Private Sub ADD_PA()
Dim StrSQL_vault As String = "INSERT INTO [wvAccount] ([acName], [acPassword], [acKeyFile], [acLastLogin], [acLastModified]) VALUES (@acName, @acPassword, @acKeyFile, @acLastLogin, @acLastModified)"
classlibrary_vault = New ConnectionLibrary_Vault
classlibrary_vault.openConnection_vault(strconnection_vault)
If Not classlibrary_vault.isConnectionOpen_vault() Then
Exit Sub
End If
classlibrary_vault.initializeCommand_vault(StrSQL_vault)
classlibrary_vault.addParameter_vault("@acName", SqlDbType.NVarChar, txtName.TextLength, txtName.Text)
classlibrary_vault.addParameter_vault("@acPassword", SqlDbType.NVarChar, txtEncryptedCode.TextLength, txtEncryptedCode.Text)
classlibrary_vault.addParameter_vault("@acKeyFile", SqlDbType.NVarChar, txtKeyFile.TextLength, txtKeyFile.Text)
classlibrary_vault.addParameter_vault("@acLastLogin", SqlDbType.NVarChar, txtLastLogin.TextLength, txtLastLogin.Text)
classlibrary_vault.addParameter_vault("@acLastModified", SqlDbType.NVarChar, txtLastModified.TextLength, txtLastModified.Text)
Try
objcommand_vault.ExecuteNonQuery()
txtAccountName.Text = txtName.Text
'Set controls
_Main.NotifyIcon.Visible = True
_Main.NotifyIcon.ShowBalloonTip(1, Application.ProductName, VAULT_CREATED, ToolTipIcon.Info)
Me.ParentForm.Controls.Remove(Me)
controlManagePasswords()
Catch ex As Exception
DevComponents.DotNetBar.MessageBoxEx.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
At first when I had a primary key on my wvAccount table, I experienced no problem but now I keep getting
Database cannot be null, the empty string, or string of only
whitespace.
I have no problem creating the database, the only thing bothers me is when I try to insert a data on wvAccount. Btw, I’m using SQL Server CE 4.0 and VB.Net
The .wv extension allows me to read and write on my database previously, so probably, there’s no problem with that.
Solved it myself. I forgot to initiate the same connection string I used to create the database, instead I used the connection string for different database. Dumb me. I really have to sleep now.
Dim ds As String = "Data Source=" + (My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\" + (Application.ProductName) + "\" + (DBName) + ".wv" + ";Persist Security Info=True;"