Solved

null values as a check before insert into sql database

Posted on 2014-10-06
3
259 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 33

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

770 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