vb.net data not saved sql compact database when after closing the application - vb.net

I did small application, to save insert data to table....
The application is not saving the data after the application close..
Can any one tell me what is the wrong in the code.
Imports System.Data
Imports System.Data.SqlServerCe
Public Class Form1
Public connstring As New String("Data Source=|DataDirectory|\test_db.sdf")
Public sqlconn As New SqlCeConnection(connstring)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name As New String("jaifar")
Dim myCommand As New SqlCeCommand("Insert Into table1 (id) Values('" + name + "')", sqlconn)
If sqlconn.State = Data.ConnectionState.Closed Then sqlconn.Open()
myCommand.ExecuteNonQuery()
Me.Table1TableAdapter.Fill(Me.Test_dbDataSet1.table1)
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
sqlconn.Close()
sqlconn = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Test_dbDataSet1.table1' table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.Test_dbDataSet1.table1)
End Sub
End Class

There are few ways to work around this:
If you select local database file in the Solution Explorer window, you will see a property called ‘Copy to Output’ in the Properties window. By default it is set to ‘Copy Always’ which means that on each build, the data files in the project folder will be copied to the output folder overwriting the existing data files if any. You can set this property to Copy Never and then manually put a copy of the data file in the output folder. This way, on subsequent builds, the project system will leave the datafile in the output folder and not try to overwrite it with the one from the project. The downside is that you still have two copies so after you modify the database file using the app, if you want to work on those changes in the project, you need to copy it to the project manually and vise-versa.
You can leave the data file outside the project and create a connection to it in Database Explorer. When the IDE asks you to bring the file into the project, just say no. This way, both the design-time and the run-time will be using the same data file but the downside is that the path in the connection string will be hard coded and therefore it’ll be harder to share the project and deploy the app. Before deploying the app, just make sure to replace the full path in the settings with a relative path.
Source: https://arnulfo.wordpress.com/2007/07/21/visual-studio-and-local-databases/

after
myCommand.ExecuteNonQuery()
then write
sqlconn.Close()
Cause, when you have write execute, better close it.
You must know that saving to dataset with database is not same.. Dataset is temporaly in your program, mean still not get change in database.
and, where is our + name + value??
Like,
mycommand.addwithvalue("name", the value place)

Related

How to keep textbox data when copying a form to another computer?

I have a form with two textboxes and two buttons, I want the data in the two text boxes to be saved when the computer shuts down or when the form containing the two textboxes is copied to another computer.
Savedata1
Public Class Form1
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
My.Settings.SaveTitle = TextBox1.Text
My.Settings.SaveBody = TextBox2.Text
MsgBox("Saved textbox data")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.SaveTitle
TextBox2.Text = My.Settings.SaveBody
End Sub
Private Sub EndButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndButton.Click
Me.Close()
End Sub
End Class
With the above code, I can only save data when the computer shuts down, but when copying the form to another computer the data in the textbox is not saved.
Can you show me how to keep textbox data when copying form to another computer?
Thank you for your help.
Are you wanting the data from the TextBoxes to be shared among all copies of the application? Like if you change the values on one computer, they update on the others?
Or, do you want the data to be persistent on an application copy instance? Like if I have a copy and change the values, the person I share the application with shares my values?
If you want the data to be consistent across all instances, use a webserver to store the values (either in a data store or as a simple XML file), and have each instance download the values on startup, or at regular intervals.
If the values are to be set by you, the developer, you could simply recompile the application with the new values and let the ClickOnce deployment system handle all the client updates for you.
If you want the values set on a copy per copy basis, you will have to include a file containing the values with the application, so each new user gets the application and the settings file as part of the package.

How to save a changed label when you exit

In my program I have a preview, and edit side.
When you edit using the text boxes on the edit side(right side) and click "save", It should change the label on the right side (preview side). Although when you exit the program and re-open, all the data you entered has disappeared!,
I have tried the below code and had no luck as my result.
Public Class Form1
Private Shared NameBasic As Integer
Public Sub New()
InitializeComponent()
lblNameBasic.Text = Convert.ToString(NameBasic)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
NameBasic = txtFirstBasic.Text
lblNameBasic.Text = Convert.ToString(NameBasic)
End Sub
End Class
Also my goal is to have it be able to take it on a flashdrive onto any computer and have that data still saved within the exe. Is this even manageable? (I am more of a web based programmer so I am a bit new to this)
You need to write values to a text file at the same location of the exe. and read it. you will need both exe and the textfile.
OR
You will need to write a dll on run-time and write some values while closing the application. this will be more secure then the text file. here you will need both exe and dll to be copied into flash drive
OR
the application setting as spoken by others..

VB 2013: how to update database using datagridview values

I have a DGV in my form that can be edited. I want to write back the updated values to the database so that they are available for next read.
I searched for answers around net and found couple of ways of doing it by opening a connection and updating it. However, i was wondering if there is any direct way of updating the database by using a single command.
I am a novice at VB and learning it only for a library project, hence find it difficult to understand and implement statements that deal with SQLs in the code. pls help... thanks
The proper approach to this is to use a data adapter to populate a DataTable that you then bind to the grid, then use the same data adapter to save the changes from that table back to the database. E.g.
Private adapter As New SqlDataAdapter("SELECT * FROM MyTable", "connection string here")
Private builder As New SqlCommandBuilder(adapter)
Private table As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Query the database and populate the DataTable.
adapter.Fill(table)
'Bind the data to the UI.
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
BindingSource1.EndEdit()
'Save the changes back to the database.
adapter.Update(table)
End Sub
The BindingSource would be added to the form in the designer.

VB 2013 Persistent User Settings

On my form I have a menu with "file-save". When I click save I want to save particular settings to restore when the form is closed and re-opened. I've done this successfully for text in text-boxes and the checked states of check-boxes, but I'm failing when trying to loop through the items in a list-box. Please see below for what I've tried...
When I click save:
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs)
Handles SaveToolStripMenuItem.Click
For Each i In ListBox1.Items()
My.Settings.ListBox1.Add(i)
Next
My.Settings.Save()
End Sub
When my form loads:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
For Each i In My.Settings.ListBox1()
ListBox1.Items.Add(i)
Next
End Sub
I've only been using VB for three days, so apologies if I am missing something simple ha! Thanks for any help!!!
There is one small glitch with the StringCollection in settings. If you do not seed it with a fake variable then it starts out as Nothing and you cannot add to Nothing. in your form load add this:
' if the collection has not been initialized, do so
If My.Settings.ListBox1 Is Nothing Then
My.Settings.ListBox1= New System.Collections.Specialized.StringCollection
End If
' now it is safe to use: load strings from Setting -> form listbox
For Each s As String In My.Settings.ListBox1()
ListBox1.Items.Add(s)
Next
The very first time it runs, there are likely no saved settings, so we have to create the container for them, basically.
Option Strict can be implemented by file, by adding this at the top:
Option Strict On
Or for the project: Project => Properties => Compile: Option Strict is likely to the right (I have 2012). You can also set it as a permanent option (recommended).
Among other things, this will prevent you from plucking variables out of the air and use them without declaring a type (which will lead to errors). For instance:
For Each i In My.Settings.ListBox1()
becomes
For Each s As String In My.Settings.ListBox1() ' tell the compiler the Type

Auto Complete Text Box from Access database

I have a text box "Textbox1" and a set of 30,000 words stored in an access database. I would like to set the VB Textbox1's auto complete source to the access database. How do I do this in vb.net? I am a novice programmer at present.
Sample:
From an access Database
Create a DataSet in your vb project, connected to that database
Add New Item -> Data -> DataSet
In your .xsd designer, add a new TableAdapter, connect it to your Access file, create a query.
Query and add them to the TextBox.AutoCompleteCustomSource
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'NamesDataSet.Names' table. You can move, or remove it, as needed.
Me.NamesTableAdapter.Fill(Me.NamesDataSet.Names)
'get my names from the dataset
Dim myNames = From n In NamesDataSet.Names Select n.Name
TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
'add names to custom list
TextBox1.AutoCompleteCustomSource.AddRange(myNames.ToArray())
End Sub