Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在VB.net中将Datagridview值传递给另一个窗体_Sql_Vb.net_Ms Access - Fatal编程技术网

Sql 在VB.net中将Datagridview值传递给另一个窗体

Sql 在VB.net中将Datagridview值传递给另一个窗体,sql,vb.net,ms-access,Sql,Vb.net,Ms Access,我试图通过单击表行将datagrid值传递给另一个表单,我的问题是,SELECT语句是否是正确的代码 单击表行>将结果显示到其他表单 编辑:我现在开始工作了 这是我的新代码: Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus, Phone1, Phone2, Tel, Email, HomeAdd, StreetAdd, CityAdd, Region, Re

我试图通过单击表行将datagrid值传递给另一个表单,我的问题是,SELECT语句是否是正确的代码

单击表行>将结果显示到其他表单

编辑:我现在开始工作了

这是我的新代码:

Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus, Phone1, Phone2, Tel, Email, HomeAdd, StreetAdd, CityAdd, Region, Refname1, Refname2, Refnum1, Refnum2 FROM Empinfo WHERE ID = " & Form4.MetroGrid1.CurrentRow.Cells(0).Value.ToString
之前:

Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, 
                             Gender, Dept, DEP, MStatus 
                         FROM Empinfo 
                  WHERE ID = '" & Form4.MetroGrid1.CurrentRow.Cells(0).Selected & "'"
'我的完整代码:

Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\DB1.accdb"
            Dim con As New OleDbConnection(constr)
            Dim datareader As OleDbDataReader
            Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus FROM Empinfo WHERE ID = '" & Form4.MetroGrid1.CurrentRow.Cells(0).Selected & "'"
            Dim cmd As New OleDbCommand(selectQ, con)
            con.Open()
            datareader = cmd.ExecuteReader


            If datareader.HasRows Then
                datareader.Read()
                MetroTextBox1.Text = datareader(0).ToString
                MetroTextBox2.Text = datareader(1).ToString
                MetroTextBox3.Text = datareader(2).ToString
                MetroDateTime1.Text = datareader(3).ToString
                MetroComboBox1.Text = datareader(4).ToString
                MetroComboBox2.Text = datareader(5).ToString
                MetroComboBox3.Text = datareader(6).ToString
                MetroDateTime2.Text = datareader(7).ToString
                MetroComboBox5.Text = datareader(8).ToString

                datareader.Close()
            Else
                MetroFramework.MetroMessageBox.Show(Me, "There is no data present", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            con.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
            'MetroFramework.MetroMessageBox.Show(Me, ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
当调用Form4.MetroGrid1.CurrentRow.Cells0是合法的时,运行程序时,我发现数据类型不匹配。Selected可能没有访问Form4的实例,而且我不认为。Selected是语句所需的值。最好为Form6创建一个新的子组件,将数据传递给它

Public Class Form6
  Private id As String
  Public Sub New(id As String)
    Me.id = id
  End Sub

  Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\DB1.accdb"
        Dim con As New OleDbConnection(constr)
        Dim datareader As OleDbDataReader
        Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus FROM Empinfo WHERE ID = '" & id & "'"
        Dim cmd As New OleDbCommand(selectQ, con)
        con.Open()
        datareader = cmd.ExecuteReader
        If datareader.HasRows Then
            datareader.Read()
            MetroTextBox1.Text = datareader(0).ToString
            MetroTextBox2.Text = datareader(1).ToString
            MetroTextBox3.Text = datareader(2).ToString
            MetroDateTime1.Text = datareader(3).ToString
            MetroComboBox1.Text = datareader(4).ToString
            MetroComboBox2.Text = datareader(5).ToString
            MetroComboBox3.Text = datareader(6).ToString
            MetroDateTime2.Text = datareader(7).ToString
            MetroComboBox5.Text = datareader(8).ToString
            datareader.Close()
        Else
            MetroFramework.MetroMessageBox.Show(Me, "There is no data present", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        con.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
        'MetroFramework.MetroMessageBox.Show(Me, ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
  End Sub
End Class
用法:

Dim f6 As New Form6(MetroGrid1.CurrentRow.Cells(0).Value.ToString)
f6.Show()

你的问题不清楚。是否要将查询结果传递给其他表单标题,或者是否要使用代码中其他表单的“搜索”参数运行查询?无论哪种方式,您都应该使用显式实例化的表单和SQL参数。我正在将结果传递给其他表单。您是否检查了传递的值?我现在可以自己工作了,谢谢。我这样做了:Dim selectQ As String=选择FirstName、LastName、MI、DOB、Age、Gender、DEP、DEP、MStatus、Phone1、Phone2、Tel、Email、HomeAdd、,来自Empinfo的StreetAdd、CityAdd、Region、Refname1、Refname2、Refnum1、Refnum2,其中ID=&Form4.MetroGrid1.CurrentRow.Cells0.Value.ToString