Can anyone tell me what is wrong with the following Code.

I am trying to launch a form on a different thread because the form takes 10+ seconds to load. The code I am using follows:

Public Class frmAdjustments
    Inherits System.Windows.Forms.Form
    Dim gVariant As Guid
    Dim gsUID As Guid
    Dim TheDatabase As System.Data.SqlClient.SqlConnection
    Dim CurrentUser As Guid
    Dim gApprover As Boolean
    Dim gAdministrator As Boolean
    Private demoThread As Thread = Nothing
    Private childform As frmAdjInfo
    Dim da2 As SqlDataAdapter
    Dim ds As DataSet = New DataSet
    Public Sub SetReceive(ByVal Value As Guid, ByVal Approver As Boolean, ByVal Administrator As Boolean)
        CurrentUser = Value
        gApprover = Approver
        gAdministrator = Administrator
    End Sub
    Function LaunchForm() As Integer
        Call TestThread()
        LaunchForm = Nothing
    End Function
    Private Sub TestThread()
        Me.demoThread = New Thread(New ThreadStart(AddressOf Me.ThreadProc))
        Me.demoThread.SetApartmentState(ApartmentState.STA)
        Me.demoThread.Start()
    End Sub
    Private Sub ThreadProc()
        frmNewAdjustment.SetReceive(CurrentUser)
        frmNewAdjustment.Show()
        frmNewAdjustment.Hide()
    End Sub
    Private Sub frmAdjustments_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call LaunchForm()
        If gApprover = False Then
            tsApprove.Enabled = False
            tsReject.Enabled = False
        End If
        If gAdministrator = False Then
            tsAdmin.Enabled = False
        End If
        Call PopulateData()
    End Sub
    Function PopulateData() As Integer
        Dim ConnectionString As String
        Dim strSQLBaseDataset As String
        Dim da As New SqlDataAdapter
        Dim TheDatabase2 As System.Data.SqlClient.SqlConnection

        DataGridView1.Refresh()
        ConnectionString = "Data Source=;Initial Catalog=Adjustments;User ID=;Password="
        TheDatabase2 = New SqlClient.SqlConnection(ConnectionString)

        strSQLBaseDataset = "SELECT tAdjustmentHeader.Id, CAST(YEAR(tAdjustmentHeader.AdjustmentPeriod) as CHAR(4)) + '-' + DATENAME(MONTH,tAdjustmentHeader.AdjustmentPeriod) as [Summary Month], " & _
                            "tUser.FullName AS [Created By], tAdjustmentHeader.PostedDateTime as Posted, tAdjustmentHeader.Approved, tAdjustmentHeader.TypeCode, tAdjustmentHeader.OrderID, tAdjustmentHeader.BTN, " & _
                            "tAdjustmentHeader.Description, tAdjustmentHeader.Resolution FROM tUser INNER JOIN " & _
                            "tAdjustmentHeader ON tUser.Id = tAdjustmentHeader.OwnerId WHERE tAdjustmentHeader.DeletedDateTime IS NULL Order by AdjustmentPeriod DESC,CREATIONDATETIME DESC;"

        da.SelectCommand = New SqlCommand()
        da.SelectCommand.Connection = TheDatabase2
        da.SelectCommand.CommandText = strSQLBaseDataset
        da.SelectCommand.CommandType = CommandType.Text
        da.Fill(ds, "TestAdjustments")
        Dim objDataView = New DataView(ds.Tables("TestAdjustments"))

        DataGridView1.AutoGenerateColumns = True
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "TestAdjustments"
        With Me.DataGridView1
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False
            .Columns("ID").Visible = False
        End With

        TheDatabase2.Close()
        da = Nothing
        PopulateData = Nothing

    End Function
End Class

I am basically passing a GUID using the a SetReceive property on the NewAdjustments form. When I launch this form I'm getting an error because it's not passing the setreceive value which is the GUID and mandatory to identify the user making the change to the record.

I've been wrestling with adding threading to my app unsuccessfully for a few days and could use any help from this forum.

Thanks in advance for your replies.

Frustated!!

Recommended Answers

All 2 Replies

im a bit confused about your code ...
you try to call
frmNewAdjustment.SetReceive(CurrentUser)
but you set first time CurrentUser IN the SetReceive function.
So by calling the SetReceive function CurrentUser is nothing.

im a bit confused about your code ...
you try to call
frmNewAdjustment.SetReceive(CurrentUser)
but you set first time CurrentUser IN the SetReceive function.
So by calling the SetReceive function CurrentUser is nothing.

Sorry for the incomplete post, I am using identical logic in the frmNewAdjustment form to pass the GUID from my login form to all other forms in the project. Because I don't know of a better way to do this.

Please see the following code with frmNewAdjustment:

Imports System.Data
Imports System.Data.SqlClient
Public Class frmNewAdjustment
    Inherits System.Windows.Forms.Form
    Dim TheDatabase As System.Data.SqlClient.SqlConnection
    Dim ds As DataSet = New DataSet
    Dim dsSalesCode As DataSet = New DataSet
    Dim dt As New DataTable
    Dim ggOwnerID As Guid
    Dim MonthVar As String

    Public Sub SetReceive(ByVal Value As Guid)
        ggOwnerID = Value
    End Sub

    Private Sub frmNewAdjustment_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim f As New frmAdjustments
        Dim ConnectionString As String
        Dim dsSalesCode As DataSet = New DataSet
        Dim cmd As New SqlCommand
        Dim cmd2 As New SqlCommand
        Dim cmd3 As New SqlCommand

        MonthVar = "2010-08-01"

        txtOASalesCd.Focus()
        ConnectionString = "Data Source=;Initial Catalog=;User ID=;Password="
        TheDatabase = New SqlClient.SqlConnection(ConnectionString)

        cmd.Connection = TheDatabase
        cmd.CommandText = "SELECT DISTINCT SummaryMonth FROM dbo.tSalesCodes ORDER BY SummaryMonth DESC"
        cmd.CommandType = CommandType.Text
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds, "Summary Month")


        cmd2.Connection = TheDatabase
        cmd2.CommandText = "spGetSalesCode"
        cmd2.CommandType = CommandType.StoredProcedure
        cmd2.Parameters.AddWithValue("@Period", MonthVar)
        Dim da2 As New SqlDataAdapter(cmd2)

        da2.Fill(dsSalesCode, "Init Sales Code")
        da2.Fill(dsSalesCode, "Disp Sales Code")

        cboPeriod.DropDownStyle = ComboBoxStyle.DropDownList
        With cboPeriod
            .DataSource = ds.Tables("Summary Month")
            .DisplayMember = "SummaryMonth"
            .SelectedIndex = 0
        End With

        'Fill the Sales Code Combo Box with values from my test date 6-1-2010
        ComboBox1.AutoCompleteMode = AutoCompleteMode.Append
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
        With ComboBox1
            .DataSource = dsSalesCode.Tables("Init Sales Code")
            .DisplayMember = "Sales_Code"
            .SelectedIndex = 0
        End With
        txtOASalesCd.DataBindings.Add("Text", dsSalesCode.Tables("Init Sales Code"), "Sales_Code")
        txtInitRep.DataBindings.Add("Text", dsSalesCode.Tables("Init Sales Code"), "REP_ATTUID")
        txtInitCoach.DataBindings.Add("Text", dsSalesCode.Tables("Init Sales Code"), "COACH_ATTUID")
        txtInitCL.DataBindings.Add("Text", dsSalesCode.Tables("Init Sales Code"), "SM_ATTUID")
        txtInitAVP.DataBindings.Add("Text", dsSalesCode.Tables("Init Sales Code"), "GM_ATTUID")
        txtInitVP.DataBindings.Add("Text", dsSalesCode.Tables("Init Sales Code"), "VP_ATTUID")


        'Fill the Sales Code Combo Box with values from my test date 6-1-2010
        ComboBox2.AutoCompleteMode = AutoCompleteMode.Append
        ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox2.AutoCompleteSource = AutoCompleteSource.ListItems
        With ComboBox2
            .DataSource = dsSalesCode.Tables("Disp Sales Code")
            .DisplayMember = "Sales_Code"
            .SelectedIndex = 0
        End With

        txtDispRep.DataBindings.Add("Text", dsSalesCode.Tables("Disp Sales Code"), "REP_ATTUID")
        txtDispCoach.DataBindings.Add("Text", dsSalesCode.Tables("Disp Sales Code"), "COACH_ATTUID")
        txtDispCL.DataBindings.Add("Text", dsSalesCode.Tables("Disp Sales Code"), "SM_ATTUID")
        txtDispAVP.DataBindings.Add("Text", dsSalesCode.Tables("Disp Sales Code"), "GM_ATTUID")
        txtDispVP.DataBindings.Add("Text", dsSalesCode.Tables("Disp Sales Code"), "VP_ATTUID")


        cmd3.Connection = TheDatabase
        cmd3.CommandText = "SELECT Distinct USOC, ID FROM dbo.[tUsoc] WHERE USOC IS NOT NULL ORDER BY USOC"
        cmd3.CommandType = CommandType.Text
        Dim da3 As New SqlDataAdapter(cmd3)
        da3.Fill(ds, "Product Name")

        Dim NewColumn As New DataGridViewComboBoxColumn
        Dim NewColumn2 As New DataGridViewTextBoxColumn
        Dim NewColumn3 As New DataGridViewTextBoxColumn

        NewColumn.Name = "USOC"
        NewColumn2.Name = "Order Detail ID"
        NewColumn.DataSource = ds.Tables("Product Name")

        NewColumn.DisplayMember = "USOC"
        NewColumn.ValueMember = "ID"
        dgAdjustment.Columns.Add(NewColumn)
        dgAdjustment.Columns.Add(NewColumn2)
    End Sub

As you can see in the above code I'm passing in a GUID that identifies the user and using that value to add new records to the database. I could use any design suggestions or better ways of going about this.

Let me know if you need me to post more of this project code for clarity.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.