Hi Vb.net geniuses! I am a complete beginner in programming and would like to seek assistance regarding this very small issue of mine :( It is perfectly working if the CardID is only numbers, but if I mix letters with it this error is showing "Conversion from 'string' to 'boolean' type is not valid" Can someone explain to me why is this error showing? The Query that I created(ScalarQueryCardID) is just selecting the CardID from the database, which is datatype TEXT.

Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        If txtID.Text = "" Then
            MessageBox.Show("Please Input Card ID")

        Else
            Dim cardid As String
            cardid = txtID.Text

            If Me.TbmembersTableAdapter.ScalarQueryCardID(cardid) Then
                sql = "select * from tbmembers where CardID = '" & txtID.Text & "'"
                da = New OleDb.OleDbDataAdapter(sql, con.ConnectionString)
                da.Fill(ds, "tbmembers")

                inc = 0

                txtID.Text = ds.Tables("tbmembers").Rows(inc).Item("CardID")
                txtname.Text = ds.Tables("tbmembers").Rows(inc).Item("FullName")
                txtbalance.Text = ds.Tables("tbmembers").Rows(inc).Item("Balance")
                btnadd.Enabled = True
                txtID.Enabled = False
            Else
                MessageBox.Show("No Record Found")
                txtID.Clear()
                txtID.Enabled = True
            End If
        End If
    End Sub

Hi
I suspect it could be this line:
If Me.TbmembersTableAdapter.ScalarQueryCardID(cardid) Then
You say the .ScalarQueryCardID is just returning the CardID as a string? Then I think you should be using:
If Me.TbmembersTableAdapter.ScalarQueryCardID(cardid) <> "" Then

Without knowing more about .ScalarQueryCardID I can't really comment.

Thanks G_Waddell for your reply, Now im getting more confused. The <> "" you provided actually works, but how come the conversion from String to Boolean error is showing if I put letters in cardid?

It is because you were using an if clause which expects something it can evaluate as true or false

if TRUE Then
    'do this
Else
    'do that
End if

So you where returning a string which the code cannot evaluate as either TRUE or FALSE. What I did was to change the code to basically say:
IF the result of the ScalarQueryCardID is not equal to (<>) an Empty string ("") then

I suspect the code is don't a boolean conversion on the numeric value i.e. 0 = False 1 = true why that would be I don't know - do you have Option Explicit enabled?

Edited 3 Years Ago by G_Waddell

The format of an If statement is

If <expression> Then

where <expression is expected to be something that eventually boils down to a boolean. For example

If str = "abc" Then

the expression

str = "abc"

when evaluated results in either the value True or False. Your statement

If Me.TbmembersTableAdapter.ScalarQueryCardID(cardid) Then

does not result in a boolean value and generates the error because it cannot be converted to one.

G_Waddell - It supposed to be true since it is already existing in the Database. And if not, it should fall under the else condition. Anyway im glad for your immediate response, thank you sir :)

Reverend Jim - Sir it is actually working if the entry are numbers only, but if I add a character or letter in it, the error is showing. Maybe it cannot read alphanumeric values from the database

A numeric value can be automatically converted to a boolean. A value of zero converts to False and any non-zero value converts to True. There is no obvious conversion of strings to boolean.

Edited 3 Years Ago by Reverend Jim

This question has already been answered. Start a new discussion instead.