Hello all,
I am setting up my test program where if you enter in text into the textboxes and hit add it will add the information
to the database. If I hit clear it clears out the window in the datagridview. When I hit reload, it just reloads the old data, not the changes that were made. When I exit the program and re run the new data is there.
On my form I have a Datagridview1 and 4 textboxes and 4 bottons. The buttons are (Add,Delete,Clear,Reload)
I have Database1DataSet, TableBindingSource, TableTableAdapter
I have the 3 buttons (Add,Delete,Clear) working. But I can not get the Reload button to work.
Thanks for any help. I am pulling my hair out trying to figure this out.
Imports System.Data.SqlClient
Public Class Form1
Dim myconnection As SqlConnection
Dim mycommand As SqlCommand
Dim dr As SqlDataReader
Dim dr1 As SqlDataReader
Public imagechange As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myconnection = New SqlConnection("Data Source=(LocalDB)\v11.0 ; AttachDbFilename='C:\Users\Brian\Documents\visual studio 2013\Projects\WindowsApplication2\WindowsApplication2\Database1.mdf' ; Initial Catalog=Database1.mdf;Integrated Security=True")
Me.TableTableAdapter.Fill(Me.Database1DataSet.Table)
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles add.Click
myconnection.Open()
mycommand = New SqlCommand("insert into dbo.[Table] ([Id],[PlanetX],[PlanetY],[PlanetName]) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')", myconnection)
mycommand.ExecuteNonQuery()
MessageBox.Show("New Row Inserted")
myconnection.Close()
MsgBox("Succesfull")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles delete.Click
myconnection.Open()
mycommand = New SqlCommand("delete from dbo.[Table] WHERE [Id] = ('" & TextBox1.Text & "')", myconnection)
mycommand.ExecuteNonQuery()
MessageBox.Show("New Row Deleted")
myconnection.Close()
MsgBox("Succesfull")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Clear.Click
Database1DataSet.Clear()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles reload.Click
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = TableBindingSource
Me.TableTableAdapter.Fill(Me.Database1DataSet.Table)
End Sub
End Class
解决方案Hello,
I would suggest looking at my MSDN article Adding new records into SQL-Server table and update a DataGridView in real time. In the code for the article I do one load, you
could re-load the same way, adding data is best practice for adding a row and returning the new auto-increment primary key. I don't show a delete but by following the code within it is not difficult to figure out the delete.