Solved

null values as a check before insert into sql database

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
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.

948 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

23 Experts available now in Live!

Get 1:1 Help Now