2 Replies - 538 Views - Last Post: 23 August 2010 - 08:22 PM Rate Topic: -----

#1 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Form hangs problem when inserting data

Posted 21 August 2010 - 04:04 PM

Hi,

How to solve a form hangs problem during on inserting data to the database?

Can someone help me with the following codes?

Thank you!

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      


        Dim table As New DataTable()

        table.Columns.Add("xxx")
        table.Columns.Add("xxx")
        table.Columns.Add("xxx")
        table.Columns.Add("xxx")
 


        Dim parser As New FileIO.TextFieldParser(OpenFileDialog1.FileName.ToString())

        parser.Delimiters = New String() {","}
        parser.HasFieldsEnclosedInQuotes = True
        parser.TrimWhiteSpace = True


        parser.ReadLine()



        Do Until parser.EndOfData = True
            table.Rows.Add(parser.ReadFields())
        Loop


        Dim strSql As String = "INSERT INTO Report (xxx,xxx......) VALUES (@xxx,@xxx,@xxx.......)" & "Update Report Set xxx= UPPER(xxx)"

        Dim SqlconnectionString As String = "Data Source=xxxxx\SQLEXPRESS;Initial Catalog=master;User Id=xxxx;Password=xxxxx;"

        Using connection As New SqlClient.SqlConnection(SqlconnectionString)
            Try
                Dim cmd As New SqlClient.SqlCommand(strSql, connection)
                With cmd.Parameters
                    .Add("@xxx", SqlDbType.VarChar, 50, "xxx")
                    .Add("@xxx", SqlDbType.VarChar, 50, "xxx")
                    .Add("@xxx", SqlDbType.VarChar, 50, "xxx")
                    .Add("@xxx", SqlDbType.VarChar, 50, "xxx")
                  
                End With

                Dim adapter As New SqlClient.SqlDataAdapter()
                adapter.InsertCommand = cmd


                Dim iRowsInserted As Int32 = _
                    adapter.Update(table)

            Catch eSql As System.Data.SqlClient.SqlException
                MessageBox.Show(eSql.ToString)

            End Try

            MsgBox("Successfull Insert " & (OpenFileDialog1.FileName.ToString()))

        End Using
        Me.Close()
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Form hangs problem when inserting data

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • Joined: 02-July 08

Re: Form hangs problem when inserting data

Posted 21 August 2010 - 06:47 PM

A simple thread example would look like:

'in your click event
Dim saveThread As New System.Threading.Thread(AddressOf SaveData)
saveThread.Start()
'...

Private Sub SaveData()
'all that code in here
End Sub 


As long as you don't try to access controls or the form itself in this thread without a delegate your fine. So the Me.Close will need a delegate.

This post has been edited by hawkvalley1: 21 August 2010 - 08:23 PM

Was This Post Helpful? 1
  • +
  • -

#3 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: Form hangs problem when inserting data

Posted 23 August 2010 - 08:22 PM

it works thank you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1