The Simplest CRUD Operations for Lookup Tables in ASP.NET - Dealing with DML Operations: continued
(Page 6 of 7 )
The heart of the application is the "save" button. Let us go through the following code first:
PrivateSub btnSave_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSave.Click
If Not Me.txtDeptno.Enabled Then 'edit mode
Try
'update it
db.SQLExecute("update dept set dname='" &
Me.txtDname.Text & "', loc='" & Me.txtLocation.Text & "' where
deptno=" & Me.txtDeptno.Text)
Catch ex As Exception 'any other error
Me.lblMsg.Text = ex.Message
Me.lblMsg.Visible = True
Exit Sub
End Try
Else 'add new mode
'validation for unique deptno
If db.getRowValue("select 'exists' from dept where
deptno=" & Me.txtDeptno.Text) = "exists" Then
Me.lblMsg.Text = "Dept already exists"
Me.lblMsg.Visible = True
Exit Sub
End If
'proceeding to insert
Try
db.SQLExecute("insert into dept
(deptno,dname,loc) values (" & Me.txtDeptno.Text & ",'" &
Me.txtDname.Text & "','" & Me.txtLocation.Text & "')")
Catch ex As Exception 'any other error
Me.lblMsg.Text = ex.Message
Me.lblMsg.Visible = True
Exit Sub
End Try
End If
'after successfully saving...update the grid
Me.frDept.Visible = False
BindGrid()
End Sub
The above method simply separates every operation for ease in understanding. Each case works with a separate DML option. The above includes validation along with the operations.
Next: Points to note >>
More ASP.NET Articles
More By Jagadish Chaterjee