将值从一种形式传递到另一种形式的问题

时间:2013-08-26 13:17:36

标签: vb.net forms

我是VB.net的新手。所以这就是我想要做的事情,我遇到了一些严重的问题 使用“Get_Computer_Name表单”会将用户名和帐户类型发布到另一个表单中。 Get_Computer_Name还获取将其发送到模块的计算机名称,该模块将用作主连接。现在我试图分离登录表单和获取计算机名称但不起作用的表单(我也想尝试该方法)。

我当前的方法存在的问题是它一次只能执行一件事。它无法发布用户名和帐户类型,并且无法将计算机名称发送到模块。现在奇怪的是有一种方法。如果我注释掉帖子用户名和帐户类型,计算机名称将被发送到模块,模块将工作并连接到数据库。现在,如果我选择不注释将用户名和帐户发布到其他表单的代码,则计算机名称将不会发送到处理连接的模块,从而无法连接到数据库。

我愿意接受其他建议,而不是这样做。

谢谢

'Here is my code for "Get_Computer_Name form" 

Imports System.Data.SqlClient

Public Class Get_Computer_Name

    Public Sub Get_Computer_Name_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cmbcomputername.Text = My.Computer.Name
        connect()
    End Sub

    Private Sub cmbcomputername_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbcomputername.SelectedIndexChanged, cmbcomputername.TextChanged
        cmbcomputername.Text = My.Computer.Name
    End Sub

    Public Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnlogin.Click
        datasource = cmbcomputername.Text

        If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If

        Dim reader As SqlDataReader
        Dim sqlstatement As SqlCommand = New SqlCommand
        sqlstatement.Connection = connection

        Try
            sqlstatement.CommandText = "Select account_username, account_password, account_type " &
            "from account_login where account_username='" & txtusername.Text & "' and account_password='" & txtpassword.Text & "'"

            reader = sqlstatement.ExecuteReader()

            If (reader.Read()) Then


                Dim application_form As application_form
                application_form = New application_form
                application_form.Show()
                application_form = Nothing
                Me.Visible = False

                'This is the code that post the username and account type to the other form (application form). If I comment this code out, the system successfully connects to the database

                application_form.lblusername.Text = (reader("account_username").ToString)
                application_form.lblaccount_type.Text = (reader("account_type").ToString)

                sqlstatement.Dispose()
                reader.Close()
                connection.Close()

            Else
                connection.Close()
                MsgBox("Invalid")
            End If
            reader.Close()
        Catch ex As Exception

        End Try
    End Sub
End Class

'This is the code for the module that holds the main connection string

    Public connection As SqlConnection = New SqlConnection
    Public datasource As String
    Public database As String = "bpmi"
    Public sqlmainconnector As String

    Public Sub connect()
        sqlmainconnector = "Data Source=" & datasource & ";Initial Catalog=bpmi;Integrated Security=True"
        connection.ConnectionString = sqlmainconnector

        Try

            If connection.State = ConnectionState.Closed Then
                connection.Open()
            Else
                connection.Close()
            End If

        Catch ex As Exception

        End Try

    End Sub

'And this code is the form where the username and account type will be posted

Imports System.Data.SqlClient

Public Class application_form

    Dim sqlcommand As SqlCommand
    Dim da As SqlDataAdapter
    Dim table As New DataTable
    Dim dategrabberapp As String
    Dim dategrabberben As String
    Dim benidgrabber As String

    Private Sub Form1_Load(sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        connect()

        filldatagrid(updateappdatagrid, updatebendatagrid)
        fillcomboboxstatus(cmbstatus)
        fillcomboboxposition(cmbappworkposition)
        fillcomboboxrelationship(cmbrelation)
        fillcomboboxrelationshipupdate(cmbrelationbenupdate)
        fillcomboboxstatusupdate(cmbappstatusupdate)
        fillcomboboxpositionupdate(cmbappworkposupdate)

'this is where the account type will got to

        If lblaccount_type.Text <> "Administrator" Then

            Me.TabControl1.TabPages(1).Enabled = False
            Me.TabControl1.TabPages(2).Enabled = False

            MsgBox("As a 'Staff', you are not permitted to do any updates")
            updateappdatagrid.Hide()
            updatebendatagrid.Hide()
        End If

    End Sub

End Class

0 个答案:

没有答案
相关问题