Introducing LINQ to SQL Designer using Visual Studio 2008 - Creating a simple LINQ to SQL application: updating information to the database
(Page 3 of 5 )
Let us start adding a new web page to the project.
Add a new web page (.aspx) to your project
Design your web page to look like the following (Fig 08):

Modify your code behind to look like the following:
Imports System.Data.Linq
Partial Public Class CRUDSample
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.lblMsg.Text = ""
End Sub
Protected Sub btnSelectStar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSelectStar.Click
Dim db As New SampleDBDataContext
Me.GridView1.DataSource = db.emps
Me.GridView1.DataBind()
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
Try
Dim db As New SampleDBDataContext
'getting an existing row
Dim objEmp As emp = db.emps.SingleOrDefault(Function(p) p.empno = Me.txtEmpno.Text)
If objEmp IsNot Nothing Then
Me.txtEname.Text = objEmp.ename
Me.txtSal.Text = objEmp.sal
Me.txtDeptno.Text = objEmp.deptno
Else
Me.lblMsg.Text = "Employee not found"
End If
Catch ex As Exception
Me.lblMsg.Text = ex.Message
End Try
End Sub
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsert.Click
Try
Dim db As New SampleDBDataContext
'adding a new row
Dim rEmp As emp = New emp With {.empno = Me.txtEmpno.Text, .ename = Me.txtEname.Text, .sal = Me.txtSal.Text, .deptno = Me.txtDeptno.Text}
db.emps.InsertOnSubmit(rEmp)
'saving rows added
db.SubmitChanges()
Me.lblMsg.Text = "Added Successfully"
Catch ex As Exception
Me.lblMsg.Text = ex.Message
End Try
End Sub
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
Try
Dim db As New SampleDBDataContext
'getting an existing row
Dim objEmp As emp = db.Emps.SingleOrDefault(Function(p) p.empno = Me.txtEmpno.Text)
If objEmp IsNot Nothing Then
'modifying the row
objEmp.ename = Me.txtEname.Text
objEmp.sal = Me.txtSal.Text
objEmp.deptno = Me.txtDeptno.Text
'saving rows added
db.SubmitChanges()
Me.lblMsg.Text = "Updated Successfully"
Else
Me.lblMsg.Text = "Employee not found"
End If
Catch ex As Exception
Me.lblMsg.Text = ex.Message
End Try
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
Try
Dim db As New SampleDBDataContext
'getting an exiting row
Dim objEmp As emp = db.emps.SingleOrDefault(Function(p) p.empno = Me.txtEmpno.Text)
If objEmp IsNot Nothing Then
'delete the row
db.emps.DeleteOnSubmit(objEmp)
'saving rows deleted
db.SubmitChanges()
Me.lblMsg.Text = "Deleted Successfully"
Else
Me.lblMsg.Text = "Employee not found"
End If
Catch ex As Exception
Me.lblMsg.Text = ex.Message
End Try
End Sub
End Class
Now you can observe that the above code is bit different from my previous article.
How about dealing with transactions? Does "LINQ to SQL" support transactions? The next section gives you the answer.
Next: Creating a simple LINQ to SQL application: handling transactions using LINQ >>
More .NET Articles
More By Jagadish Chaterjee