Question:
visual basic 2008 saving a database plss help?
Third
2013-06-14 03:17:48 UTC
im new on vb.net so please help me solve my problem,here's my problem ..my program is supposed to be like this : a human record with age,name and sex , also includes add new,delete record and save record, apparently i did this coding thing but relaying on a tutorial video ..all functions are working but there's one thing this one thing >,< here if i click the save button it will definitely save the new record to the datagridview and it will appear there but if i close the vb2008 and reopens the project the data that i recently save in not there :( can you help me make the code that if i click the save button the record will be save in the access dbs on my desktop and will show even if the application is closed: heres my code>>>>
Public Class Form1

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 'InventDataSet.Table1' table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.InventDataSet.Table1)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Table1BindingSource.MoveNext()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Table1BindingSource.MovePrevious()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Table1BindingSource.AddNew()

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Table1BindingSource.EndEdit()
Table1TableAdapter.Update(InventDataSet.Table1)


End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Table1BindingSource.RemoveCurrent()
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class
Three answers:
Black Nine
2013-06-14 03:31:31 UTC
I think the best way is to use OleDBAdapter class. You use the DataAdapter's Fill() method to read the contents of a table in a database and fill our locally-cached DataTable object.



There are 3 steps to saving data in ADO.Net:

•Get a locally cached copy of the data with the Fill() method

•Write code (or use a control) that makes changes to the locally cached copy

•Save the changes back to the underlying database with the Update() method



Here's the code,

Using conn As New OleDBConnection("connectionString Here")

Using comm As New OleDBCommand()

With comm

.Connection = conn

.CommandType = CommandType.CommandText

.CommandText = "SELECT * FROM youTableName"

End With

Using adapter As New OleDBDataAdapter(comm)

Dim _dataTable As New DataTable()

adapter.Fill(_dataTable)



'add you records here '

' preferably by using loop '

Dim _dataRow As DataRow

_dataRow = _dataTable.NewRow()

_dataRow("colNameA") = "valueA"

_dataRow("colNameB") = "valueB"

'........ '

_dataTable.Rows.Add(_dataRow)



Dim dt_changes As DataTable

dt_changes = _dataTable.Changes()

If Not IsNothing(dt_changes) Then

Using commBuild As OleDbCommandBuilder(adapter)

Dim rowCount as Integer = adapter.Update(dt_changes)

MsgBox(rowCount & " updated")

End Using

End If

End Using

End Using

End Using
Blackened
2013-06-14 03:33:02 UTC
You code is probably fine. Most likely your database file is being copied over when you debug your program. Select your database file (the file ending in .sdf or .dbf in your solution explorer and change its 'copy to output directory' property to 'do not copy'. Just make sure that if you make changes to the structure of your database that you'll want to change that property back to its previous value ('copy if newer') so that the changes will be visible to the program while you're debugging.
kaspari
2016-10-22 06:07:33 UTC
you take advantage of an adventure to set off code that takes whats contained in the textual content boox and appends it right into a textual content report. There are numerous activities you should use so choosing which adventure to set off one relies upon upon what you as a programmer and your customers opt for to do. some activities to judge KeyUp KeyDown KeyPressed GotFocus LostFocus confirmed TextChanged different controls like a timer might want to correctly be used to do a periodic keep depending upon a time reduce. keep each 10 seconds as an example. those evets are solid applicants to judge, which one you opt for relies upon upon precisely the way you opt for your application to artwork. EDIT: depending upon which adventure you opt for you may have unintended consequences jointly with triggering a keep for each letter being entered. evaluate typing "hi" right into a textual content field you should keep "hi" in a report. yet when the shape handler fires each and every time you enter a letter your textual content report might want to seem as if H He Hel Hell hi OR H e l l o neither of those is what i think you intend


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...