从列表框选择中填充文本框 [英] Populate textboxes from the listbox selection

查看:94
本文介绍了从列表框选择中填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,当它运行时,MDB数据库将名称填充到ListBox中。我破坏了我的大脑试图在列表框中选择一个名称和完整信息,然后填充文本框。最后我希望使用文本框来向数据库添加新记录。任何帮助表示赞赏,这是我的代码,注释掉的代码是不起作用的。

 Imports System.Data 
Imports System.Data.OleDb

Public Class Form1
Private Sub Button1_Click(sender As Object,e As EventArgs)处理Button1.Click
Dim strConnection As String =Provider = Microsoft.Jet.OLEDB.4.0; Data源= C:\Temp\Hospital.mdb;
Dim objConnection As New OleDbConnection(strConnection)
Dim objConnection2 As New OleDbConnection(strConnection)

'打开错误处理连接
尝试
objConnection。 Open()
Catch OleDbExceptionErr As OleDbException
MessageBox.Show(OleDbExceptionErr.Message)
Catch InvalidOperationErr As InvalidOperationException
MessageBox.Show(InvalidOperationErr.Message)
End Try

'使用选择名字所需的SQL语句创建一个命令对象
Dim strSQL As String =SELECT * FROM Patients
Dim objCommand As New OleDbCommand(strSQL,objConnection) )

'创建数据适配器和数据表然后填充数据表
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New DataTable(PatientID)
objDataAdapter.Fill (objDataTable)

'创建连接和释放资源
objConnection.Close()
objConnection.Dispose()
objConnection = Nothing
objCommand.Dispose( )
objCommand = Nothing
objDataAdapter.Dispose()
objDataAdapter = Nothing

'将名称填入列表框
For Each Row As DataRow in objDataTable。行
lstNames.Items.Add(Row.Item(患者姓名))
下一个

'释放资源
objDataTable.Dispose()
objDataTable = Nothing
End Sub

Public Sub Button2_Click(sender As Object,e As EventArgs)处理Button2.Click

'Dim strConnection As String =Provider = Microsoft.Jet.OLEDB.4.0;数据源= C:\Temp \ Hospital.mdb;

'Dim DR As DataRow =选择*来自PatientID ='& lstNames.SelectedValue& '
'txtName.Text = DR(患者姓名)
'txtWard.Text = DR(病房)
'txtNotes.Text = DR(注释)

结束子





我尝试过:



'Dim DR As DataRow =选择*来自PatientID ='& lstNames.SelectedValue& '
'txtName.Text = DR(患者姓名)
'txtWard.Text = DR(病房)
'txtNotes.Text = DR(注释)

解决方案

您需要的是通过lstnames选择值从Patients表中读取并将其他字段值传递给textboxes

'从connectionString创建连接

Dim objConnection As New OleDbConnection(Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Temp \\ \ ndospital.mdb;)
'创建命令

Dim objCommand As New OleDbCommand(选择[患者姓名],[病房],[注释]来自患者ID ='的患者& lstNames.SelectedValue&',objConnection)
Dim reader As OleDbDataReader
'打开连接

objConnection.open()
'执行查询

reader = command.ExecuteReader()

'在访问数据之前始终调用Read。
虽然reader.Read()
txtName.Text = reader.GetValue(0)
txtWard.Text = reader.GetValue(1)
txtNotes.Text = reader.GetValue( 2)
结束时
reader.close





如需更多文档,请阅读 MSDN文档 - OleDbDataReader.Read方法


I have a small program and when it runs the MDB database populates the Names into the ListBox. I have wrecked my brain trying to select a name in the Listbox and the full information to then populate the text Boxes. Eventually I wish to use the text Boxes to add new records to the database. Any help appreciated, here is my code, the commented out code is what won't work.

Imports System.Data
Imports System.Data.OleDb

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\Hospital.mdb;"
        Dim objConnection As New OleDbConnection(strConnection)
        Dim objConnection2 As New OleDbConnection(strConnection)

        'Open the connection with error handling
        Try
            objConnection.Open()
        Catch OleDbExceptionErr As OleDbException
            MessageBox.Show(OleDbExceptionErr.Message)
        Catch InvalidOperationErr As InvalidOperationException
            MessageBox.Show(InvalidOperationErr.Message)
        End Try

        'Create a command object with the SQL statement needed to select the first names
        Dim strSQL As String = "SELECT * FROM Patients"
        Dim objCommand As New OleDbCommand(strSQL, objConnection)

        'Create a data adapter and data table then fill the data table
        Dim objDataAdapter As New OleDbDataAdapter(objCommand)
        Dim objDataTable As New DataTable("PatientID")
        objDataAdapter.Fill(objDataTable)

        'Create connection and release resources
        objConnection.Close()
        objConnection.Dispose()
        objConnection = Nothing
        objCommand.Dispose()
        objCommand = Nothing
        objDataAdapter.Dispose()
        objDataAdapter = Nothing

        'Fill names into the listbox
        For Each Row As DataRow In objDataTable.Rows
            lstNames.Items.Add(Row.Item("Patient Name"))
        Next

        'Release resources
        objDataTable.Dispose()
        objDataTable = Nothing
    End Sub

    Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        'Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\Hospital.mdb;"

        'Dim DR As DataRow = Select * From Patients  Where PatientID = "'" & lstNames.SelectedValue & "'"
        'txtName.Text = DR("Patient Name")
        'txtWard.Text = DR("Ward")
        'txtNotes.Text = DR("Notes")

    End Sub



What I have tried:

'Dim DR As DataRow = Select * From Patients  Where PatientID = "'" & lstNames.SelectedValue & "'"
       'txtName.Text = DR("Patient Name")
       'txtWard.Text = DR("Ward")
       'txtNotes.Text = DR("Notes")

解决方案

Which you need is to read from Patients table by lstnames selected value and pass others fields value to textboxes

'Create Connection from connectionString

Dim objConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\Hospital.mdb;")
'Create a command

Dim objCommand As New OleDbCommand("Select [Patient Name],[Ward],[Notes] From Patients  Where PatientID ='" & lstNames.SelectedValue & "'", objConnection)
Dim reader As OleDbDataReader
'Open connection

objConnection.open()
'Execute query

reader = command.ExecuteReader()

' Always call Read before accessing data.
While reader.Read()
    txtName.Text=reader.GetValue(0)
    txtWard.Text = reader.GetValue(1)
    txtNotes.Text = reader.GetValue(2)
End While
reader.close



For more documentation please read MSDN Documentation - OleDbDataReader.Read Method


这篇关于从列表框选择中填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆