Solved

null values as a check before insert into sql database

Posted on 2014-10-06
3
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.NET Redemption/Outlook Examples 3 35
.NET universe documentation poster 2 26
VB .net 2010 Byte array 2 24
Sql server query 8 21
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

752 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