How to commit inserted data?

davy_g

Well-known member
Joined
Jul 18, 2007
Messages
63
Location
Belgium
Programming Experience
Beginner
Hi all,

I'm pretty new in VB .NET and just wanted to create a simple application which can read, insert, update and delete data into a MS SQL Server 2005.

I've managed to insert the data via SqlCommand and read it via the DataAdapter, but when I close my application and open it again, I see that my inserted data has gone.
I need to have a way to commit this insert, but how do I do it?

My code:

Imports System.Data
Imports System.Data.SqlClient


Public Class Form1
Dim con As New SqlConnection
Dim Command As SqlCommand
Dim ds As New DataSet
Dim da As SqlDataAdapter
Dim sql As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\test.mdf;User ID=sa;Password=Admin1;Connect Timeout=30;User Instance=False"
con.Open()
MsgBox("succesfully opened database!")
con.Close()

End Sub

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

con.Open()

Command = New SqlCommand("INSERT Table_1 (id, naam) " & "VALUES (@id, @naam) ")
Command.Parameters.Add("@id", TextBox1.Text)
Command.Parameters.Add("@naam", TextBox2.Text)
Command.Connection = con
Command.ExecuteNonQuery()
MsgBox("Data inserted")

con.Close()

End Sub

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

con.Open()
sql = "SELECT id, naam FROM Table_1"
da = New SqlDataAdapter(sql, con)
da.Fill(ds, "Testie")

con.Close()

TextBox1.Text = ds.Tables("Testie").Rows(0).Item(0)
TextBox2.Text = ds.Tables("Testie").Rows(0).Item(1)

End Sub
End Class


It's a simple form with 2 textboxes (textbox1 andf textbox2) and 2 buttons (button1 to insert, button 2 to read).

Can you please help me?
I've tried to do something with ContextUtil.SetComplete() but this does not seam to work (perhaps I'm doing it all wrong).

Many thanks!
 
I just found the problem on your forum :)

I've changed Copy Always to Copy if Newer and it works.

So I've not asked anything here :)
 
Davy, youre making your life way harder than it needs to be.. Take a read of the DW2 link in my signature; section "creating a simple data app"
 
Back
Top