Solved

null values as a check before insert into sql database

Posted on 2014-10-06
3
246 Views
Last Modified: 2014-10-06
Hey I have a sub that check for null values and I have it working in other vb projects. but for so reason aia'm getting an A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

maybe there is a better way to handle textboxs for null values before you Insert them into a database But I don't understand why it works in past projects and not the current project this is the function it's the ="" or

 Public Sub InsertCheck()
        txtFirstname.BackColor = Color.WhiteSmoke
        txtLastname.BackColor = Color.WhiteSmoke
        txtPosition.BackColor = Color.WhiteSmoke
        TXTteamID.BackColor = Color.WhiteSmoke
        txtPlayerID.BackColor = Color.WhiteSmoke

        If btnNew.Text = "Save" Then
            txtDPosition.Text = "" Or txtDraftteam.Text = "" Or txtDraftYear.Text = "" Or txtorigteamID.Text = "" Or txtOverallP.Text & _
                txtRound.Text = ""
            MessageBox.Show("Fill in all yellow textboxs")
            btnNew.Text = "Add New"
            txtFirstname.BackColor = Color.LightYellow
            txtLastname.BackColor = Color.LightYellow
            txtPosition.BackColor = Color.LightYellow
            TXTteamID.BackColor = Color.LightYellow
            txtPlayerID.BackColor = Color.LightYellow
            Exit Sub

        End If

    End Sub

Open in new window

0
Comment
Question by:powerztom
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 40364035
If btnNew.Text = "Save" Then
            txtDPosition.Text = "" Or txtDraftteam.Text = "" Or txtDraftYear.Text = "" Or txtorigteamID.Text = "" Or txtOverallP.Text & _
                txtRound.Text = ""


Is it a typo in your code here or is code like this in your project as well? Should probably be

If btnNew.Text = "Save"  AND _
            (txtDPosition.Text = "" Or txtDraftteam.Text = "" Or txtDraftYear.Text = "" Or txtorigteamID.Text = "" Or txtOverallP.Text & _
                txtRound.Text = "")
0
 

Author Closing Comment

by:powerztom
ID: 40364070
CodeCrusier thanks for quick service your CODE worked fine. Thank You
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40364181
I agree with CodeCruiser, there is a typo in your code.  I, however, would suggest a change.  It looks as if you are wanting to validate before allowing the user to save.  Normally you would only want to highlight what is missing and not all of the text boxes.

Something like this:
Public Sub InsertCheck()
        If btnNew.Text = "Save" Then
            txtFirstName.BackColor = IIf(String.IsNullOrEmpty(txtFirstName.Text), Color.LightYellow, Color.WhiteSmoke)
            txtLastName.BackColor = IIf(String.IsNullOrEmpty(txtLastName.Text), Color.LightYellow, Color.WhiteSmoke)
            txtPosition.BackColor = IIf(String.IsNullOrEmpty(txtPosition.Text), Color.LightYellow, Color.WhiteSmoke)
            TXTteamID.BackColor = IIf(String.IsNullOrEmpty(TXTteamID.Text), Color.LightYellow, Color.WhiteSmoke)
            txtPlayerID.BackColor = IIf(String.IsNullOrEmpty(txtPlayerID.Text), Color.LightYellow, Color.WhiteSmoke)
            MessageBox.Show("Fill in all yellow textboxs")
            btnNew.Text = "Add New"
            Exit Sub
        Else
                txtFirstname.BackColor = txtLastname.BackColor = txtPosition.BackColor = TXTteamID.BackColor = txtPlayerID.BackColor = Color.WhiteSmoke
        End If
End Sub

Open in new window


Althought, the better way would be to make use of your events:

Something like this:
Public Sub InsertCheck(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFirstName.TextChanged, txtLastName.TextChanged, txtPosition.TextChanged, TXTteamID.TextChanged, txtPlayerID.TextChanged
        If btnNew.Text = "Save" AndAlso TypeOf sender Is TextBox Then
                Dim tb As TextBox = TryCast(sender, TextBox)
                If tb = txtFirstName OrElse tb = txtLastName OrElse tb = txtPosition OrElse tb = TXTteamID OrElse tb = txtPlayerID
                        tb.BackColor = IIf(String.IsNullOrEmpty(tb.Text), Color.LightYellow, Color.WhiteSmoke)
                        MessageBox.Show("Fill in all yellow textboxs")
                        btnNew.Text = "Add New"
                End If
        Else
                txtFirstname.BackColor = txtLastname.BackColor = txtPosition.BackColor = TXTteamID.BackColor = txtPlayerID.BackColor = Color.WhiteSmoke
        End If
End Sub

Open in new window


-saige-
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now