swethajain
Well-known member
- Joined
- Feb 1, 2010
- Messages
- 48
- Programming Experience
- Beginner
Hi
I used error provider tool to display the error when the textbox is empty. when the error message is displayed immediately it is inserting the record into the database.I want to clear the error and then insert. how to take the control to textbox?Please help me out.........
Imports System.Data.OleDb
Public Class Form1
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim ra As Integer
Dim result As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ErrorProvider1.ContainerControl = Me
TextBox2.Text = Environment.UserName.ToString()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ValidateForm()
Try
Dim id As String = TextBox1.Text
Dim dt As String = DateTimePicker1.Text
Dim re As String = TextBox2.Text
Dim cp As String = ComboBox1.SelectedItem.ToString
Dim rg As String = ComboBox2.SelectedItem.ToString
Dim tr As String = ComboBox3.SelectedItem.ToString
Dim lo As String = TextBox3.Text
Dim nn As String = TextBox4.Text
Dim ta As String = TextBox5.Text
Dim pr As String = TextBox6.Text
Dim du As String = TextBox7.Text
Dim ass As String = TextBox8.Text
Dim bStatus As Boolean = True
myConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='h:\info2.mdb'")
myConnection.Open()
Dim str As String = "insert into Resource([ID],[Dt],[Res],[Construction Package],[Region],[Trade],[Location],[Network Name],[Task],[Progress],[Duration],[Assumptions])values ('" & id & "','" & dt & "','" & re & "','" & cp & "','" & rg & "','" & tr & "','" & lo & "','" & nn & "','" & ta & "','" & pr & "','" & id & "','" & ass & "')"
Dim cmd As New OleDbCommand(str, myConnection)
ra = cmd.ExecuteNonQuery()
If (ra > 0) Then
result = MsgBox("Record Inserted", MsgBoxStyle.Information, "Message Box")
End If
myConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
For Each ctrl As Control In Me.Controls
If ctrl.GetType.Name = "TextBox" Then ctrl.Text = ""
If ctrl.GetType.Name = "ComboBox" Then ctrl.Text = ""
Next
TextBox2.Text = Environment.UserName.ToString()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
End Sub
Private Sub textBox5_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Validatetextbox5()
End Sub
Private Sub textBox7_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Validatetextbox7()
End Sub
Private Function Validatetextbox5() As Boolean
Dim bStatus As Boolean = True
If TextBox5.Text = "" Then
ErrorProvider1.SetError(TextBox5, "Please enter the task")
bStatus = False
Else
ErrorProvider1.SetError(TextBox5, "")
End If
Return (bStatus)
End Function
Private Function Validatetextbox7() As Boolean
Dim bStatus As Boolean = True
If TextBox7.Text = "" Then
ErrorProvider1.SetError(TextBox7, "Please enter the duration")
bStatus = False
Else
ErrorProvider1.SetError(TextBox7, "")
Try
Dim temp As Integer = Integer.Parse(TextBox7.Text)
ErrorProvider1.SetError(TextBox7, "")
If temp > 17 Then
ErrorProvider1.SetError(TextBox7, "Duration should not be more than 16Hrs")
bStatus = False
Else
ErrorProvider1.SetError(TextBox7, "")
End If
Catch
ErrorProvider1.SetError(TextBox7, "Please enter a number")
bStatus = False
End Try
End If
Return (bStatus)
End Function
Private Sub ValidateForm()
Dim bValidText As Boolean = Validatetextbox5()
Dim bValidDuration As Boolean = Validatetextbox7()
If bValidText AndAlso bValidDuration Then
MessageBox.Show("Data entered is correct")
Else
MessageBox.Show("Please enter valid data")
End If
End Sub
End Class
I used error provider tool to display the error when the textbox is empty. when the error message is displayed immediately it is inserting the record into the database.I want to clear the error and then insert. how to take the control to textbox?Please help me out.........
Imports System.Data.OleDb
Public Class Form1
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim ra As Integer
Dim result As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ErrorProvider1.ContainerControl = Me
TextBox2.Text = Environment.UserName.ToString()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ValidateForm()
Try
Dim id As String = TextBox1.Text
Dim dt As String = DateTimePicker1.Text
Dim re As String = TextBox2.Text
Dim cp As String = ComboBox1.SelectedItem.ToString
Dim rg As String = ComboBox2.SelectedItem.ToString
Dim tr As String = ComboBox3.SelectedItem.ToString
Dim lo As String = TextBox3.Text
Dim nn As String = TextBox4.Text
Dim ta As String = TextBox5.Text
Dim pr As String = TextBox6.Text
Dim du As String = TextBox7.Text
Dim ass As String = TextBox8.Text
Dim bStatus As Boolean = True
myConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='h:\info2.mdb'")
myConnection.Open()
Dim str As String = "insert into Resource([ID],[Dt],[Res],[Construction Package],[Region],[Trade],[Location],[Network Name],[Task],[Progress],[Duration],[Assumptions])values ('" & id & "','" & dt & "','" & re & "','" & cp & "','" & rg & "','" & tr & "','" & lo & "','" & nn & "','" & ta & "','" & pr & "','" & id & "','" & ass & "')"
Dim cmd As New OleDbCommand(str, myConnection)
ra = cmd.ExecuteNonQuery()
If (ra > 0) Then
result = MsgBox("Record Inserted", MsgBoxStyle.Information, "Message Box")
End If
myConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
For Each ctrl As Control In Me.Controls
If ctrl.GetType.Name = "TextBox" Then ctrl.Text = ""
If ctrl.GetType.Name = "ComboBox" Then ctrl.Text = ""
Next
TextBox2.Text = Environment.UserName.ToString()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
End Sub
Private Sub textBox5_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Validatetextbox5()
End Sub
Private Sub textBox7_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Validatetextbox7()
End Sub
Private Function Validatetextbox5() As Boolean
Dim bStatus As Boolean = True
If TextBox5.Text = "" Then
ErrorProvider1.SetError(TextBox5, "Please enter the task")
bStatus = False
Else
ErrorProvider1.SetError(TextBox5, "")
End If
Return (bStatus)
End Function
Private Function Validatetextbox7() As Boolean
Dim bStatus As Boolean = True
If TextBox7.Text = "" Then
ErrorProvider1.SetError(TextBox7, "Please enter the duration")
bStatus = False
Else
ErrorProvider1.SetError(TextBox7, "")
Try
Dim temp As Integer = Integer.Parse(TextBox7.Text)
ErrorProvider1.SetError(TextBox7, "")
If temp > 17 Then
ErrorProvider1.SetError(TextBox7, "Duration should not be more than 16Hrs")
bStatus = False
Else
ErrorProvider1.SetError(TextBox7, "")
End If
Catch
ErrorProvider1.SetError(TextBox7, "Please enter a number")
bStatus = False
End Try
End If
Return (bStatus)
End Function
Private Sub ValidateForm()
Dim bValidText As Boolean = Validatetextbox5()
Dim bValidDuration As Boolean = Validatetextbox7()
If bValidText AndAlso bValidDuration Then
MessageBox.Show("Data entered is correct")
Else
MessageBox.Show("Please enter valid data")
End If
End Sub
End Class