从combobox.selectedvalue填充文本框 [英] populate textboxes from combobox.selectedvalue

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

问题描述

G'day



我终于将我的数据库从访问SQL转移到了 - 但对于我需要编写新GUI的世界感到高兴。我快到那里了。它比我想象的容易得多。



我不太明白如何从我的select查询中检索值以将它们放入文本框中。



我想我对它有一个基本的了解,但有人能告诉我,如果我的想法是错误的吗?



我需要调整我想从select query第一个接收的变量。

我需要运行SQL查询来接收where子句后所需的值。



但是,如何引用这些值将它们放入变量中,然后显示我的文本框。



基本上它是一个修复具有主键(RA)的数据库,它只会带来一个结果。



在combobox.selectedindexchanged上,它需要运行sql查询我输入然后需要将其放入变量中。



我认为值将存储为qrystr.row或somethi这种性质?我是对的吗?



到目前为止我所拥有的是



G'day

I'm finally moving my DB from access to SQL - but joy to the world I need to write a new GUI. I'm getting there. It's a lot easier than I imagined.

I don't quite understand how I can retrieve values from my select query to place them into textboxes.

I think I have a basic understanding of it, but could someone tell me if I'm thinking the wrong way?

I need to dim the variables I want to receive from the select query 1st.
I need to run the SQL query to receive the values required after the where clause.

But, how do I reference the values to put them into the variables, which would then show I the textboxes.

Basically it's a repairs database that has a Primary Key (the RA) and it will only ever bring across one result.

On combobox.selectedindexchanged, it needs to run through the sql query that I've typed in and then it needs to be put into the variables.

I'm thinking the values would be stored as qrystr.row or something of that nature? Am I correct?

What I have so far is below

Private Sub RA_IDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RA_IDComboBox.SelectedIndexChanged
       Dim connectionstring As New SqlConnection(Main.connectionstring)
       Dim qrystr As String = "SELECT        RA.RA_ID, RA.Till_ID, HardwareType.TillType, RA.Date_S, RA.Date_R, RA.Quote, RA.Accept, Supplier.Supplier, RA.Our_Cost, RA.Price, Customer.Company, " & _
                                            "Courier.CourCompany, RA.SendCon, RA.FixCon, RA.HIssue" & _
                                            "FROM            Courier INNER JOIN " & _
                                            "RA ON Courier.CourierID = RA.CourCompany INNER JOIN" & _
                                            "Customer ON RA.Company = Customer.CustomerID INNER JOIN" & _
                                            "Supplier ON RA.Supplier = Supplier.SupplierID INNER JOIN" & _
                                            "HardwareType ON RA.TillType = HardwareType.ID" & _
                                            "WHERE        (RA.RA_ID = N' " & RA_IDComboBox.Text & "')"
       Dim TillID As String
       Dim tilltype As String
       Dim customer As String
       Dim dates As String
       Dim supplier As String
       Dim courier As String
       Dim sendcon As String
       Dim hissue As String

推荐答案

您可以使用数据适配器从DB中选择/更新数据



1.声明您的连接

2.声明SQL数据适配器

3.声明数据表

4.填充数据表





这是修改后的代码



Private Sub RA_IDComboBox_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)处理RA_IDComboBox.SelectedIndexChanged



''我已经删除了SQL注入

''使用参数总是

Dim qrystr As String =SEL ECT RA.RA_ID,RA.Till_ID,HardwareType.TillType,RA.Date_S,RA.Date_R,RA.Quote,RA.Accept,Supplier.Supplier,RA.Our_Cost,RA.Price,Customer.Company,&_

Courier.CourCompany,RA.SendCon,RA.FixCon,RA.HIssue&_

FROM Courier INNER JOIN&_

RA ON Courier.CourierID = RA.CourCompany INNER JOIN&_

客户ON RA.Company = Customer.CustomerID INNER JOIN&_

供应商RA。 Supplier = Supplier.SupplierID INNER JOIN&_

HardwareType ON RA.TillType = HardwareType.ID&_

WHERE(RA.RA_ID = @RA_ID)





Dim TillID As String

Dim tilltype As String

Dim customer As String

Dim dates As String

Dim supplier As String

Dim courier As String

Dim sendcon As String

Dim hissue As String

使用con作为新的SqlConnection(Main.connectionstring)''声明连接

使用da As New SqlDataAdapter(qrystr,con)''声明数据适配器

''添加参数并增加值

da.SelectCommand .Parameters.Add(@ RA_ID,SqlDbType.Int).Value = CInt(RA_IDComboBox.SelectedValue)

使用dtbl作为新DataTable''声明数据表

da .Fill(dtbl)''获取数据

如果dtbl.Rows.Count> 0然后

''您在数据表中有数据

''因为您只需要选择命令中的1条记录

''您可以使用dtbl.Rows(0)



TillID = dtbl.Rows(0)(TillID)。ToString()



tilltype = dtbl.Rows(0)(TillType)。ToString()







结束如果





结束使用



结束使用



结束使用



End Sub
You can use data adapter to select/update data from DB

1. Declare your connection
2. Declare SQL data adapter
3. Declare data table
4. Fill the datatable


Here is the modified code

Private Sub RA_IDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RA_IDComboBox.SelectedIndexChanged

''I have removed SQL injection
''Use parameters always
Dim qrystr As String = "SELECT RA.RA_ID, RA.Till_ID, HardwareType.TillType, RA.Date_S, RA.Date_R, RA.Quote, RA.Accept, Supplier.Supplier, RA.Our_Cost, RA.Price, Customer.Company, " & _
"Courier.CourCompany, RA.SendCon, RA.FixCon, RA.HIssue" & _
"FROM Courier INNER JOIN " & _
"RA ON Courier.CourierID = RA.CourCompany INNER JOIN" & _
"Customer ON RA.Company = Customer.CustomerID INNER JOIN" & _
"Supplier ON RA.Supplier = Supplier.SupplierID INNER JOIN" & _
"HardwareType ON RA.TillType = HardwareType.ID" & _
"WHERE (RA.RA_ID = @RA_ID)"


Dim TillID As String
Dim tilltype As String
Dim customer As String
Dim dates As String
Dim supplier As String
Dim courier As String
Dim sendcon As String
Dim hissue As String
Using con As New SqlConnection(Main.connectionstring) ''Declare connection
Using da As New SqlDataAdapter(qrystr, con) ''Declare data adapter
''Add parameter and add value
da.SelectCommand.Parameters.Add("@RA_ID", SqlDbType.Int).Value = CInt(RA_IDComboBox.SelectedValue)
Using dtbl As New DataTable ''Declare data table
da.Fill(dtbl) ''Get data
If dtbl.Rows.Count > 0 Then
''You have data in datatable
''since you expect only 1 records from the select command
''You can use dtbl.Rows(0)

TillID = dtbl.Rows(0)("TillID").ToString()

tilltype = dtbl.Rows(0)("TillType").ToString()



End If


End Using

End Using

End Using

End Sub


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

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