Click here to Skip to main content
15,914,160 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Imports System.Threading
Public Class Form1
    Public thread1 As New System.Threading.Thread(New ThreadStart(AddressOf connecttodata.search))
    Dim tic As Integer = 0

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        connecttodata.join()
        connecttodata.insert()
    End Sub

    Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
        connecttodata.join()
        thread1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        tic += 1
        If tic = 1 Then
            lbl1.Visible = True
            lbl2.Visible = False
            lbl3.Visible = False
        ElseIf tic = 2 Then
            lbl1.Visible = True
            lbl2.Visible = True
            lbl3.Visible = False
        Else
            lbl1.Visible = True
            lbl2.Visible = True
            lbl3.Visible = True
            tic = 0
        End If
        lbl1.Update()
        lbl2.Update()
        lbl3.Update()
    End Sub
End Class

'Module connect
Module connect
    Public Class connecttodata
        Public Shared cnnoledb As New OleDbConnection
        Public Shared cmdinsert As New OleDbCommand
        Public Shared cmdoledb As New OleDbCommand
        Public Shared rdroledb As OleDbDataReader
        Public Shared Sub join()
            Try
                Dim strconnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.Environment.CurrentDirectory _
                & "\data.mdb;Jet OLEDB:Database Password=ehsan"
                cnnoledb.ConnectionString = strconnection
                cnnoledb.Open()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Public Shared Sub close()
            cnnoledb.Close()
            cnnoledb.Dispose()
        End Sub
        Public Shared Sub insert()
            cmdinsert.CommandText = "INSERT INTO table1 (ID,NAME) VALUES(" & Form1.txtnumber.Text & ", '" & _
            Form1.txtname.Text & " ')"
            cmdinsert.CommandType = CommandType.Text
            cmdinsert.Connection = cnnoledb
            cmdinsert.ExecuteNonQuery()
            MsgBox("inserted records")
            cmdinsert.Dispose()
            close()
        End Sub
        Public Shared Sub search()
            Try
                cmdoledb.CommandText = "SELECT NAME FROM table1 WHERE ID=" & CInt(Form1.txtid.Text)
                cmdoledb.CommandType = CommandType.Text
                cmdoledb.Connection = connecttodata.cnnoledb
                rdroledb = cmdoledb.ExecuteReader
                While rdroledb.Read = True
                    Form1.txtnam.Text &= rdroledb.Item(0).ToString
                    Thread.Sleep(200)
                    tictac()
                End While
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            cmdoledb.Dispose()
            connecttodata.close()
            Form1.Timer1.Enabled = False
            Form1.thread1.Abort()
        End Sub
        Public Shared Sub tictac()
            If Form1.Timer1.Enabled = False Then
                Form1.Timer1.Enabled = True
            End If
        End Sub
    End Class
End Module


-------------------------My question--------------------
'I keep getting this Following error:
'conversion from string "" to type "integer" is not valid.'
'note:If I do not use thread,An error does not have any.
Posted
Updated 18-May-10 12:15pm
v2
Comments
Sandeep Mewara 19-May-10 2:15am    
You have not told where exactly that error occurs. Please specify the same.

Hi ehsan6000,

You CANNOT call the user interface from your thread and should use invoke to the UI to sync up with the main thread. Just google "vb.net Thread Invoke UI".

Good luck!
 
Share this answer
 
You've used Invoke and Invoke required before :http://www.codeproject.com/Questions/82446/A-small-error-thread.aspx[^]

and we told you how to fix that problem. Why are you reverting back to old ways and not using it?

The only place that I can see where you would get that error is:
VB
cmdoledb.CommandText = "SELECT NAME FROM table1 WHERE ID=" & CInt(Form1.txtid.Text)


If Form1.txtid.Text is not a valid integer value then you would get this error. It looks like in this case, that Form1.txtid.Text is blank.

You would be better using TryParse like this:

VB
Dim intTemp as Integer

If Integer.TryParse(Form1.txtid.Text,intTemp)
    cmdoledb.CommandText = "SELECT NAME FROM table1 WHERE ID=" & intTemp
Else
    connecttodata.Close()
    Return
End If


You also don't need to abort the thread. When the function ends, so will the thread. Telling it to abort as the last thing is unnecessary.

You also don't need to test boolean values against true or false (as in
VB
While rdroledb.Read = True


If it already is a boolean value, then testing it again a boolean is just adding extra processing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900