Question InvalidCastException - Trying to search database

amecily

New member
Joined
Sep 29, 2010
Messages
1
Programming Experience
1-3
Hi everyone,

I am using Visual Studio 2005, Version 2.0. Coding a windows application with VB.NET.

I am trying to use a textbox (txtLogCall) to display matched results in a datagrid. I have added a bindingsource and have the dataset and tableadapter.

I have set the databinding tag of the datagrid to the field that I want to use as a parameter. The textbox is for an Caller's Name, so I have set the datagrid tag to 'EmergencyCallerIdentification' that is in the table.

I have created a SQL query:

VB.NET:
SELECT        EmergencyCallID, EmergencyResponseID, PriorityCode, EmergencyCallTime, EmergencyCallerIdentification, EmergencyPriority, 
                         EmergencyShortDescription, Landmarks, LocationDetails, NumberOfPatients, PatientsState, EmergencyCallerCallBackNumber, EmergencyCallState
FROM            [Emergency Call]
WHERE        (EmergencyCallerIdentification = ?) and (EmergencyCallerIdentification LIKE '::EmergencyCallerIdentification::')

And the code I am using is:

VB.NET:
 If Not txtLogCall.Text = "" Then
            If IsNumeric(txtLogCall.Text) Then
                MessageBox.Show("Name must be text only", "Invalid Name")
                txtLogCall.Focus()
                txtLogCall.BackColor = Color.Red
                txtLogCall.Select(0, txtLogCall.Text.Length)
                Exit Sub
            Else : txtLogCall.BackColor = Color.White

                DsEmergencyCall.Emergency_Call.Clear()

                Try
                    Me.Emergency_CallTableAdapter1.Fill(DsEmergencyCall.Emergency_Call, txtLogCall.Text)

                Catch ex As Exception
                    System.Windows.Forms.MessageBox.Show(ex.Message)


                End Try

                dgName.Visible = True

                txtLogCall.Text = dgName.Tag


                Dim parameter
                parameter = dgName.Tag
                Me.Emergency_CallTableAdapter1.Fill(DsEmergencyCall.Emergency_Call, parameter)


            End If

        End If

The error I get when I try to search in this textbox is InvalidCastException - Conversion from type 'DBNull' to type 'String' is not valid. The line of code that is highlighted for this error is 'txtLogCall.Text = dgName.Tag'

The above code did work perfectly for my other textbox search, but it requires the user to enter numeric values.

How can I resolve this?

Thanks,
Amy
 
Erm.. First off, how can this:

WHERE (EmergencyCallerIdentification = ?) and (EmergencyCallerIdentification LIKE '::EmergencyCallerIdentification::')

Be true for anything other than a value of '::EmergencyCallerIdentification::'
i.e. the parameter there is useless


The only thing I can think is that the value of dgName.Tag is DBNull and that cannot be converted to a string. Actually, the whole thing is confusing, because you fill once and fill again seemingly uselessly.. I can't see where you set dgName.Tag to a value so I can't really comment. It would appear that VB is calling ToString() or casting for you, so enabling Options Strict and Option Explicit would also be sensible steps, then soe of the black magic going on behind the scenes is brought out

I'd actually advocate stopping coding and writing some plain english in comments of what you want your code to do, step by step. work out the logic at the high level and then translate it into code.
Remember: weeks of coding can save you hours of planning! :)
 
Back
Top