take the control back to textbox after display error by error provider tool

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 would suggest naming the control something valuable and not just the default designer names. Like:
VB.NET:
txtUserId.Text = UserName
'not textbox1.Text = UserName
 
Hi
Thank you soo much for your replies but i want the control to got to the error textbox so that i can type the correct data.Please help me out............
 
You know what control it is since your setting the error provider, just add a line of code to also set the focus to that textbox at the same time.

VB.NET:
If TextBox5.Text = "" Then
        ErrorProvider1.SetError(TextBox5, "Please enter the task")
        bStatus = False
[B]        TextBox5.Focus( )[/B]
Else
        ErrorProvider1.SetError(TextBox5, "")
End If
 
Hi
Thank you soo mush for the reply. can u please give tell me the code for how to validate a textbox immediately when i press tab and go to next box.
 
In that case you would most likely want to use the control's lost focus event but I dislike checking at this time and instead prefer checking all fields at once such as when they click an OK or Save button. For one reason, you would still need to check these controls at some point because they still may skip the control without it ever having gained focus.
 
Back
Top