akosiadrian
New member
- Joined
- Feb 11, 2009
- Messages
- 1
- Programming Experience
- Beginner
Hi guys, good day to all of you. I have created a program that add,edit, delete items on the database. Im using SQL 2005 as my backend. Im having problems creating or adding a search function for my program. Im using dataset because im not familiar with hard code stuff. I will be posting my entire code and hopefully some pics to help us out. Thanks guys
==========================================================
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub SetControls(ByVal valueBoolean As Boolean)
'This sub procedure sets the user interface for the
'BindingNavigator control and bound controls for Edit/Add
'operations
'ReadOnly/Not ReadOnly the bound controls
MiddleNameTextBox.Enabled = Not valueBoolean
LastNameTextBox.Enabled = Not valueBoolean
FirstNameTextBox.Enabled = Not valueBoolean
AddressTextBox.Enabled = Not valueBoolean
SerialTextBox.Enabled = Not valueBoolean
StatusComboBox.Enabled = Not valueBoolean
MeterTextBox.Enabled = Not valueBoolean
NearestTextBox.Enabled = Not valueBoolean
'Make the Move, Position, and Buttons
'(except Save and Cancel) Enable/disable
FirstButton.Enabled = valueBoolean
NextButton.Enabled = valueBoolean
PreviousButton.Enabled = valueBoolean
LastButton.Enabled = valueBoolean
AddButton.Enabled = valueBoolean
EditButton.Enabled = valueBoolean
DeleteButton.Enabled = valueBoolean
'Enable/disable the Save and Cancel Buttons
SaveButton.Enabled = Not valueBoolean
CancelButton.Enabled = Not valueBoolean
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MeretDataSet2.Meret_main' table. You can move, or remove it, as needed.
Me.Meret_mainTableAdapter1.Fill(Me.MeretDataSet2.Meret_main)
'TODO: This line of code loads data into the 'MeretDataSet1.Meret_sub' table. You can move, or remove it, as needed.
Me.Meret_subTableAdapter.Fill(Me.MeretDataSet1.Meret_sub)
Try
Me.Meret_mainTableAdapter1.Fill(Me.MeretDataSet2.Meret_main)
SetControls(True)
Catch ex As Exception
Dim messageString As String = "DataBase Connection Failed" & ControlChars.NewLine & _
"Report this error to the system developer [09062864264]." & ControlChars.NewLine & ex.Message
Dim titleString As String = "Database Connection Failure"
MessageBox.Show(messageString, titleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
SetControls(False)
MeretmainBindingSource1.AddNew()
LastnameTextBox.Focus()
End Sub
Private Sub EditButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditButton.Click
Me.SetControls(False)
End Sub
Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
Dim rowNumberInteger As Integer = Me.MeretmainBindingSource1.Position
Try
Dim responseDialogResult As DialogResult = MessageBox.Show("Confirm to delete the student record.", "Delete Y/N?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If responseDialogResult = Windows.Forms.DialogResult.Yes Then
'Delete the row by removing the current record,
'ending the edit, and calling the Update method
Me.MeretmainBindingSource1.RemoveCurrent()
Me.MeretmainBindingSource1.EndEdit()
'Me.MeretmainBindingSource.Update(Me.MeretmainDataSet.Meretmain)
Me.Meret_mainTableAdapter1.Update(Me.MeretDataSet2.Meret_main)
End If
Catch exOleDb As OleDb.OleDbException
'The deletion attempt failed due to a relationship
'to existing data rows in the ENROLLMENT table
MessageBox.Show("This student cannot be deleted - the student is enrolled in courses." & _
ControlChars.NewLine & exOleDb.Message, "Delete Operation Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
'Restore the deleted row with the RejectChanges method
Me.MeretDataSet2.RejectChanges()
'Reposition to the row that was deleted
Me.MeretmainBindingSource1.Position = rowNumberInteger
Catch ex As Exception
'Some other exception was triggered
MessageBox.Show("Unexpected error in delete operation: " & ControlChars.NewLine & _
ex.Message, "Delete Operation Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelButton.Click
Me.SetControls(True)
Me.MeretmainBindingSource1.CancelEdit()
Me.MeretDataSet2.RejectChanges()
End Sub
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
Try
Me.MeretmainBindingSource1.EndEdit()
Me.Meret_mainTableAdapter1.Update(Me.MeretDataSet2.Meret_main)
Me.SetControls(True)
Catch ex As Exception
Dim messageString As String = "Error saving into Database" & ControlChars.NewLine & ex.Message
Dim TitleString As String = "Error in Operation"
MessageBox.Show(messageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub FirstButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirstButton.Click
MeretmainBindingSource1.MoveFirst()
End Sub
Private Sub PreviousButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreviousButton.Click
MeretmainBindingSource1.MovePrevious()
End Sub
Private Sub NextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextButton.Click
MeretmainBindingSource1.MoveNext()
End Sub
Private Sub LastButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LastButton.Click
MeretmainBindingSource1.MoveLast()
End Sub
Private Sub SerialTextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles SerialTextBox.KeyPress
Select Case Asc(e.KeyChar)
Case 8, 48 To 57
e.Handled = False 'Allow the key
Case Else
e.Handled = True 'Ignore the key
End Select
End Sub
Private Sub MeterTextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MeterTextBox.KeyPress
Select Case Asc(e.KeyChar)
Case 8, 48 To 57
e.Handled = False 'Allow the key
Case Else
e.Handled = True 'Ignore the key
End Select
End Sub
End Class
==========================================================
this is the picture of my program
==========================================================
and can you teach me also how to hard code this without relying on dataset/databind?
thanks in advance guys!
==========================================================
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub SetControls(ByVal valueBoolean As Boolean)
'This sub procedure sets the user interface for the
'BindingNavigator control and bound controls for Edit/Add
'operations
'ReadOnly/Not ReadOnly the bound controls
MiddleNameTextBox.Enabled = Not valueBoolean
LastNameTextBox.Enabled = Not valueBoolean
FirstNameTextBox.Enabled = Not valueBoolean
AddressTextBox.Enabled = Not valueBoolean
SerialTextBox.Enabled = Not valueBoolean
StatusComboBox.Enabled = Not valueBoolean
MeterTextBox.Enabled = Not valueBoolean
NearestTextBox.Enabled = Not valueBoolean
'Make the Move, Position, and Buttons
'(except Save and Cancel) Enable/disable
FirstButton.Enabled = valueBoolean
NextButton.Enabled = valueBoolean
PreviousButton.Enabled = valueBoolean
LastButton.Enabled = valueBoolean
AddButton.Enabled = valueBoolean
EditButton.Enabled = valueBoolean
DeleteButton.Enabled = valueBoolean
'Enable/disable the Save and Cancel Buttons
SaveButton.Enabled = Not valueBoolean
CancelButton.Enabled = Not valueBoolean
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MeretDataSet2.Meret_main' table. You can move, or remove it, as needed.
Me.Meret_mainTableAdapter1.Fill(Me.MeretDataSet2.Meret_main)
'TODO: This line of code loads data into the 'MeretDataSet1.Meret_sub' table. You can move, or remove it, as needed.
Me.Meret_subTableAdapter.Fill(Me.MeretDataSet1.Meret_sub)
Try
Me.Meret_mainTableAdapter1.Fill(Me.MeretDataSet2.Meret_main)
SetControls(True)
Catch ex As Exception
Dim messageString As String = "DataBase Connection Failed" & ControlChars.NewLine & _
"Report this error to the system developer [09062864264]." & ControlChars.NewLine & ex.Message
Dim titleString As String = "Database Connection Failure"
MessageBox.Show(messageString, titleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
SetControls(False)
MeretmainBindingSource1.AddNew()
LastnameTextBox.Focus()
End Sub
Private Sub EditButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditButton.Click
Me.SetControls(False)
End Sub
Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
Dim rowNumberInteger As Integer = Me.MeretmainBindingSource1.Position
Try
Dim responseDialogResult As DialogResult = MessageBox.Show("Confirm to delete the student record.", "Delete Y/N?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If responseDialogResult = Windows.Forms.DialogResult.Yes Then
'Delete the row by removing the current record,
'ending the edit, and calling the Update method
Me.MeretmainBindingSource1.RemoveCurrent()
Me.MeretmainBindingSource1.EndEdit()
'Me.MeretmainBindingSource.Update(Me.MeretmainDataSet.Meretmain)
Me.Meret_mainTableAdapter1.Update(Me.MeretDataSet2.Meret_main)
End If
Catch exOleDb As OleDb.OleDbException
'The deletion attempt failed due to a relationship
'to existing data rows in the ENROLLMENT table
MessageBox.Show("This student cannot be deleted - the student is enrolled in courses." & _
ControlChars.NewLine & exOleDb.Message, "Delete Operation Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
'Restore the deleted row with the RejectChanges method
Me.MeretDataSet2.RejectChanges()
'Reposition to the row that was deleted
Me.MeretmainBindingSource1.Position = rowNumberInteger
Catch ex As Exception
'Some other exception was triggered
MessageBox.Show("Unexpected error in delete operation: " & ControlChars.NewLine & _
ex.Message, "Delete Operation Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelButton.Click
Me.SetControls(True)
Me.MeretmainBindingSource1.CancelEdit()
Me.MeretDataSet2.RejectChanges()
End Sub
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
Try
Me.MeretmainBindingSource1.EndEdit()
Me.Meret_mainTableAdapter1.Update(Me.MeretDataSet2.Meret_main)
Me.SetControls(True)
Catch ex As Exception
Dim messageString As String = "Error saving into Database" & ControlChars.NewLine & ex.Message
Dim TitleString As String = "Error in Operation"
MessageBox.Show(messageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub FirstButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirstButton.Click
MeretmainBindingSource1.MoveFirst()
End Sub
Private Sub PreviousButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreviousButton.Click
MeretmainBindingSource1.MovePrevious()
End Sub
Private Sub NextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextButton.Click
MeretmainBindingSource1.MoveNext()
End Sub
Private Sub LastButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LastButton.Click
MeretmainBindingSource1.MoveLast()
End Sub
Private Sub SerialTextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles SerialTextBox.KeyPress
Select Case Asc(e.KeyChar)
Case 8, 48 To 57
e.Handled = False 'Allow the key
Case Else
e.Handled = True 'Ignore the key
End Select
End Sub
Private Sub MeterTextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MeterTextBox.KeyPress
Select Case Asc(e.KeyChar)
Case 8, 48 To 57
e.Handled = False 'Allow the key
Case Else
e.Handled = True 'Ignore the key
End Select
End Sub
End Class
==========================================================
this is the picture of my program
==========================================================
and can you teach me also how to hard code this without relying on dataset/databind?
thanks in advance guys!