liziskandar
Member
Hello all,
Could anyone check what's wrong with my code.
1. When I click the save button, it will give an error.
2. The ID textbox supposed to be readonly and showing new number whenever Add button clicked. Where as the other textboxes are empty and ready to accept new data to be save. How to do this?
Thanks in advance.
Could anyone check what's wrong with my code.
1. When I click the save button, it will give an error.
2. The ID textbox supposed to be readonly and showing new number whenever Add button clicked. Where as the other textboxes are empty and ready to accept new data to be save. How to do this?
Thanks in advance.
VB.NET:
Imports System.Data
Imports System.Data.OleDb 'This is to import the class of oledb
Public Class frmEdit1
Inherits System.Windows.Forms.Form
Dim DBCon As OleDb.OleDbConnection ' for database connection
Dim da As OleDb.OleDbDataAdapter ' adapter is use to update the dataset and datasource
Dim ds As DataSet 'miniature of your table - cache table to client
Dim CurrentRecord As Integer ' for displaying caption in datagrid
Dim editstate As Boolean ' to know if the user is in adding or editing of record
Dim strSQL As String
Private Sub frmEdit1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DBCon = New OleDb.OleDbConnection
Dim Dblocation As String = System.IO.Directory.GetCurrentDirectory & "\GESB.mdb"
DBCon.ConnectionString = "Provider=Microsoft.Jet.oledb.4.0;Data Source='" & Dblocation & "'"
DBCon.Open()
Call getdata()
Call cleartext()
disablebutton()
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDelete.Enabled = True
DISABLETEXT()
dgLogin.CaptionText = "Users"
End Sub
'Opening a Login table then passing it to a Login dataset(table cache in a client computer)
Private Sub getdata()
strSQL = "SELECT ID as [ID], UserName as [UserName], Password FROM Login ORDER BY ID, UserName ASC"
Dim cmd As New OleDbCommand(strSQL, DBCon)
ds = New DataSet
da = New OleDb.OleDbDataAdapter(cmd)
da.Fill(ds, "Login")
dgLogin.DataSource = ds.Tables("Login") 'binding of datagrid from dataset
End Sub
Private Sub DISABLETEXT()
tbID.Enabled = False
tbUserName.Enabled = False
tbPassword.Enabled = False
End Sub
Private Sub ENABLETEXT()
tbID.Enabled = True
tbUserName.Enabled = True
tbPassword.Enabled = True
End Sub
Private Sub cleartext()
tbID.Clear()
tbUserName.Clear()
tbPassword.Clear()
End Sub
Private Sub disablebutton()
btnAdd.Enabled = False
btnEdit.Enabled = False
btnCancel.Enabled = False
btnSave.Enabled = False
btnDelete.Enabled = False
End Sub
'this will remove the data bindings to textboxes
Private Sub RemoveDataBinding()
Dim c As Control
For Each c In GroupBox1.Controls
c.DataBindings.Clear()
Next c
End Sub
[COLOR=Blue] 'How to make the ID automatically increase and display in ID textbox when Add button click and after that only the new username and password will be added[/COLOR]
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
cleartext()
disablebutton()
ENABLETEXT()
editstate = False
btnSave.Enabled = True
btnCancel.Enabled = True
tbID.Text = ""
tbUserName.Focus()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
ds.Tables("Login").Rows(dgLogin.CurrentRowIndex).Delete()
Dim CommBuild As New OleDbCommandBuilder(da)
da.Update(ds, "Login")
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
ENABLETEXT()
'Binding of data to textboxes base on the current record pointer of datagrid
Me.BindingContext(ds.Tables("Login")).Position = dgLogin.CurrentRowIndex
tbID.DataBindings.Add("text", ds.Tables("Login"), "ID")
tbUserName.DataBindings.Add("text", ds.Tables("Login"), "UserName")
tbPassword.DataBindings.Add("text", ds.Tables("Login"), "Password")
disablebutton()
btnSave.Enabled = True
btnCancel.Enabled = True
RemoveDataBinding()
editstate = True
CurrentRecord = dgLogin.CurrentRowIndex
dgLogin.Enabled = False
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Select Case editstate
Case Is = False ' adding of record
Dim DrNewRow As DataRow = ds.Tables("Login").NewRow
Try
DrNewRow("ID") = tbID.Text
DrNewRow("UserName") = tbUserName.Text
DrNewRow("Password") = tbPassword.Text
ds.Tables("Login").Rows.Add(DrNewRow)
Dim CommBuild As New OleDbCommandBuilder(da)
da.Update(ds, "Login") 'updating the datasource
Catch err As Exception
MsgBox(err.Message, MsgBoxStyle.Exclamation, "error")
getdata()
Exit Sub
End Try
Case Is = True 'Editing of Record
Dim drEditrow As DataRow = ds.Tables("Login").Rows(dgLogin.CurrentRowIndex)
drEditrow.BeginEdit()
drEditrow("ID") = tbID.Text
drEditrow("UserName") = tbUserName.Text
drEditrow("Password") = tbPassword.Text
drEditrow.EndEdit()
Dim CommBuild As New OleDbCommandBuilder(da)
da.Update(ds, "Login")
End Select
If editstate = True Then
MsgBox("Record has been updated", MsgBoxStyle.Information)
dgLogin.Enabled = True
Else
MsgBox("New record has been added", MsgBoxStyle.Information)
End If
disablebutton()
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDelete.Enabled = True
cleartext()
DISABLETEXT()
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
cleartext()
DISABLETEXT()
disablebutton()
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDelete.Enabled = True
editstate = False
dgLogin.Enabled = True
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
Call getdata()
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
End Sub
Private Sub btnMoveFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
If BindingContext(ds, "Login").Position < 0 Then
With dgLogin
MsgBox("Database Is Empty!", MsgBoxStyle.Information, "GESB Business Solutions")
Exit Sub
End With
End If
Me.BindingContext(ds, "Login").Position = 0
dgLogin.UnSelect(dgLogin.CurrentRowIndex)
dgLogin.CurrentRowIndex = 0
dgLogin.Select(dgLogin.CurrentRowIndex)
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
If BindingContext(ds, "Login").Position < 0 Then
With dgLogin
MsgBox("Database Is Empty!", MsgBoxStyle.Information, "GESB Business Solutions")
Exit Sub
End With
End If
Me.BindingContext(ds, "Login").Position -= 1
dgLogin.UnSelect(dgLogin.CurrentRowIndex)
If dgLogin.CurrentRowIndex > 0 Then
dgLogin.CurrentRowIndex -= 1
End If
dgLogin.Select(dgLogin.CurrentRowIndex)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If BindingContext(ds, "Login").Position < 0 Then
With dgLogin
MsgBox("Database Is Empty!", MsgBoxStyle.Information, "GESB Business Solutions")
Exit Sub
End With
End If
Me.BindingContext(ds, "Login").Position += 1
dgLogin.UnSelect(dgLogin.CurrentRowIndex)
dgLogin.CurrentRowIndex += 1
dgLogin.Select(dgLogin.CurrentRowIndex)
End Sub
Private Sub btnMoveLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
If BindingContext(ds, "Login").Position < 0 Then
With dgLogin
MsgBox("Database Is Empty!", MsgBoxStyle.Information, "GESB Business Solutions")
Exit Sub
End With
End If
Me.BindingContext(ds, "Login").Position = Me.BindingContext(ds, "Login").Count - 1
dgLogin.UnSelect(dgLogin.CurrentRowIndex)
dgLogin.CurrentRowIndex = BindingContext(ds, "Login").Count - 1
dgLogin.Select(dgLogin.CurrentRowIndex)
End Sub
End Class