Hello All, I have a project that's been assigned to me which involves communication between the front end which is vb form and a back end which is SQL server. I am new to sql server. I wrote a sample form before using the live data to see if I would be able to achieve this or not. I was able to get the data from sql db and go through it with next and previous buttons but I can't edit, delete, or add any information or data to the database through the vb form. So any help on this would be greatly appreciated.
Code:
Code:
VB.NET:
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim con As New OleDb.OleDbConnection("Provider=sqloledb;Data Source=sql;Initial Catalog=db name;User Id=user id;Password=pw;")
Dim cb As OleDbCommandBuilder
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
da = New OleDbDataAdapter("Select * from student", con)
da.Fill(ds, "student")
con.Open()
txtname.DataBindings.Add(New System.Windows.Forms.Binding("text", ds, "student.name"))
txtroll.DataBindings.Add(New System.Windows.Forms.Binding("text", ds, "student.roll"))
txtcourse.DataBindings.Add(New System.Windows.Forms.Binding("text", ds, "student.course"))
lbl1.Text = ds.Tables("student").Rows.Count
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
BindingContext(ds, "student").AddNew()
MessageBox.Show("Successfully Inserted")
End Sub
Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
BindingContext(ds, "student").RemoveAt(BindingContext(ds, "student").Position)
da.Update(ds)
End Sub
Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
BindingContext(ds, "Student").Position = BindingContext(ds, "Student").Position + 1
End Sub
Private Sub btnprev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprev.Click
BindingContext(ds, "student").Position = BindingContext(ds, "student").Position - 1
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
Me.BindingContext(ds, "student").EndCurrentEdit()
da.Update(ds)
End Sub
Private Sub btnfirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfirst.Click
BindingContext(ds, "student").Position = 0
End Sub
Private Sub btnlast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlast.Click
BindingContext(ds, "student").Position = BindingContext(ds, "student").Count - 1
End Sub
Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
txtname.Clear()
txtroll.Clear()
txtcourse.Clear()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
con.Close()
Me.Close()
End Sub
End Class
Last edited by a moderator: