Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Theres something wrong with my codes. When i click the button to perform a lines of code, mssgbox keeps on repeating. Total items inside a groupbox that a user forgot to enter or select a value (combBox,txtBox..etc) is also the number times the mssgbox will pops out. Please help! (newbie)

VB
For Each item As Control In GroupBox1.Controls

            If item.Text = Nothing Then
                ''inform the user that all fields should have a value
                MsgBox("Please fill up all fields")
            Else
                If MsgBox("Do you want to save this record?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                    ''save the record
                End If

            End If

        Next
Posted
Comments
wsm.akh@gmail.com 6-Jan-18 13:23pm    
I am getting error message "Conversion from type 'DBNull' to type 'String' is not valid." each time while navigating the records whenever any any field comes.

My code is;

Imports System
Imports System.Data
Imports System.Data.OleDb

Public Class Form1
Dim CurrentRow As Integer
Dim DST As New DataSet
Dim ds As New DataSet
Dim dv As DataView
Dim cm As CurrencyManager
Dim dataadapter As OleDb.OleDbDataAdapter
Public CON As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Dot Net Projects\vb dot project complete example\DataMovement\DataMovement.accdb")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dataadapter = New OleDb.OleDbDataAdapter("SELECT * FROM Table1", CON)
filldatasetandview()
FormLoadKaro()
End Sub

Private Sub FormLoadKaro()
Try
CurrentRow = 0
CON.Open()
dataadapter = New OleDb.OleDbDataAdapter("SELECT * FROM Table1", CON)
dataadapter.Fill(DST, "Table1")
ShowData(CurrentRow)
CON.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

' Binding all fields
Public Sub ShowData(ByVal CurrentRow)
Try
TextBox1.Text = DST.Tables("Table1").Rows(CurrentRow)("ID")
DateTimePicker1.Text = DST.Tables("Table1").Rows(CurrentRow)("dob")
TextBox3.Text = DST.Tables("Table1").Rows(CurrentRow)("naam")
TextBox4.Text = DST.Tables("Table1").Rows(CurrentRow)("address")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

' Fill Data
Public Sub filldatasetandview()
ds = New DataSet
dataadapter.Fill(ds, "Table1")
dv = New DataView(ds.Tables("Table1"))
cm = CType(Me.BindingContext(dv), CurrencyManager)
End Sub

' Binding all fields
Public Sub bindfields()
TextBox1.DataBindings.Clear()
DateTimePicker1.DataBindings.Clear()
TextBox3.DataBindings.Clear()
TextBox4.DataBindings.Clear()

TextBox1.DataBindings.Add("text", dv, "ID")
DateTimePicker1.DataBindings.Add("text", dv, "dob")
TextBox3.DataBindings.Add("text", dv, "naam")
TextBox4.DataBindings.Add("text", dv, "address")
End Sub

Private Sub cmdPre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPre.Click
Try
If CurrentRow <> 0 Then
CurrentRow -= 1
ShowData(CurrentRow)
Else
MsgBox("This is the First Record...")
End If
Catch ex As Exception
MsgBox("No record to show")
End Try
End Sub

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
If CurrentRow = DST.Tables("Table1").Rows.Count - 1 Then
MsgBox("This is the Last Record...")
Else
CurrentRow += 1
ShowData(CurrentRow)
End If
End Sub

Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
Try
CurrentRow = 0
ShowData(CurrentRow)
Catch ex As Exception
MsgBox("There is no record to show")
End Try
End Sub

Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
CurrentRow = DST.Tables("Table1").Rows.Count - 1
ShowData(CurrentRow)
End Sub
End Class

Dear Developer,

Try the following code...

VB
nCount = 0
For Each item As Control In GroupBox1.Controls

    If item.Text = Nothing Then
      nCount = nCount + 1
    End If

Next

If nCount<>0 Then
    ''' inform the user that all fields should have a value
    MsgBox("Please fill up all fields")
Else
    If MsgBox("Do you want to save this record?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
        '''save the record
    End If

End If
 
Share this answer
 
Comments
akosisugar 22-Sep-13 14:03pm    
wow tNx! this is what im trying to say.. tHank you sir!
CHill60 23-Sep-13 16:05pm    
Only improvement I can suggest is what I think Thomas Barbare was trying to point out ... once you've found an empty text box you could just exit the For loop.
Ali198324 20-Jan-21 12:03pm    
Why did this code not work?

For Each item As Data_Entery In GroupBox1.Controls

If item.Text = Nothing Then


MsgBox("يرجى تعبئة جميع البيانات")
cmM_H.Focus()
Exit Sub
Else

If MsgBox("هل تريد حفظ البيانات", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
Dim cmd As New OleDbCommand
cmd = New OleDbCommand("Insert Into Data_Entrey ([M_H],[MR_MS],[T_G],[Modil],[Compeny],[Many_Factor],[SN],[NO],[Date],[GH_DE],[Po],[Notes]) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "', #" & DateTimePicker1.Text & "# ,'" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox11.Text & "')", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
btnSave.Enabled = False
btnNew.Enabled = True
GroupBox1.Enabled = False
Button1.Focus()

End If
End If
Next
I would suggest you to go this way:

VB
Public Sub SaveRecord()
    For Each item As Control In GroupBox1.Controls
        If String.IsNullOrEmpty(item.Text) Then
            ''inform the user that all fields should have a value
            MsgBox("Please fill up all fields............." & item.Name)
            item.Focus()
            Exit Sub
        End If
    Next
    If MsgBox("Do you want to save this record?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
        ''save the record
    End If
End Sub


The advantage of using this solution is:

1) You can point out the textbox that the user is missing or left unfilled.
2) This way you don't have to iterate to all textbox controls and make decision at the end.
3) No need of additional variable.

Hope it is useful for you since you are newbie.
 
Share this answer
 
v2
Comments
akosisugar 24-Sep-13 20:08pm    
sir thank you!
Shahan Ayyub 25-Sep-13 2:16am    
@akosibogart, I think it is more appropriate to use `String.IsNullOrEmpty` rather than `xyz = Nothing`. See the updates.
akosisugar 25-Sep-13 21:01pm    
ived noticed your solution is quiet the same to my problem. although as u hv said its pointing out the item|s that has a no value. i think i rather choose the solution and explanation of @Thomas Barbare. but anyway's i respect ur solution..thank you again!
I recommend this solution :


VB
For Each item As Control In GroupBox1.Controls

           If item.Text = Nothing Then
               ''inform the user that all fields should have a value
               MsgBox("Please fill up all fields")
               Exit Sub ' or return if it's a function
           End If
      
       Next

If MsgBox("Do you want to save this record?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                   ''save the record
End If


Because, why looking to other controls if the 1st is not correct ?
 
Share this answer
 
v5
Comments
CHill60 23-Sep-13 16:04pm    
However this solution will produce the msgbox "Do you want to save..." for each iteration of the loop where the text box *does* contain data
Thomas Barbare 23-Sep-13 16:14pm    
Oh yes you are right ! I kept the initial logic. I edit my solution
akosisugar 25-Sep-13 20:50pm    
this solution is much easier and also easy to understand. all i want is user must fill up all items inside the groupbox. thank you!
Thomas Barbare 26-Sep-13 4:32am    
You're welcome !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900