Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

null values as a check before insert into sql database

Posted on 2014-10-06
3
Medium Priority
?
285 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 2000 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 35

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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

782 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