Public Sub progselector()
Dim connection As New SqlClient.SqlConnection
connection.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\PC83-USER\Documents\Visual Studio 2013\Projects\generic_print_v2\generic_print_v2\programs.mdf;Integrated Security=True;"
Dim dr As SqlDataReader
Dim fcount As Integer
Dim fldnamecombo As String
Dim command As New SqlCommand
Dim querycommand As New SqlCommand
command.Connection = connection
command.CommandType = CommandType.Text
command.CommandText = " select field_name from field where program_name = '" & Program_nameComboBox.SelectedItem & "'; select count(field_name) from field where program_name = '" & Program_nameComboBox.SelectedItem & "' ; select field_name,field_value,sort_priority from fieldvalue where program_name ='" & Program_nameComboBox.SelectedItem & "'"
'THIS SECTION LOADS DATA FROM THE TABLES'
Try
connection.Open()
Field_nameComboBox.Items.Clear()
dr = command.ExecuteReader
While dr.Read()
fldnamecombo = dr(0)
Field_nameComboBox.Items.Add(fldnamecombo)
Field_nameComboBox.SelectedItem = fldnamecombo
Field_nameComboBox.SelectedIndex = 0
End While
dr.NextResult()
While dr.Read
fcount = dr(0)
If fcount = 0 Then
Field_nameComboBox.Text = ""
Field_nameComboBox.SelectedItem = ""
Field_valueComboBox.Items.Clear()
Field_valueComboBox.Text = ""
Field_valueComboBox.SelectedItem = "'"
lbl_fld_fieldv.Text = "0"
Sort_priorityLabel1.Visible = False
lbl_field_sortprio.Visible = False
Field_valueComboBox.Visible = False
lbl_field_fvalues.Visible = False
End If
dr.NextResult()
lbl_field_fldno.Text = fcount
End While
While dr.Read()
txtbx_field_list.Text += dr(0)
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
connection.Close()
SqlConnection.ClearPool(connection)
End Try
End Sub
部屋,
我目前遇到了txtbx_field_list行如何出现的问题。结果应该是 T1F T2F T3F
然而,由于某种原因,它只是不断重复。所以T1F T2F T3F T1F T2F T3F T1F T2F T3F。 这在Selected Index Changed Sub中调用。我必须在某处休息一下吗?或者我是以错误的方式称呼它。请帮忙!谢谢!
答案 0 :(得分:0)
你不应该使用nextresult,为什么你一遍又一遍地循环?您只是从表中获取一条信息,那么为什么不将这些信息添加到arraylist,数组,泛型列表或哈希表中(无论您喜欢什么),然后循环通过其他信息?
通常,尝试尽可能少地保持sql连接打开。这个小的循环不会影响任何东西,但是当你开始进入更复杂的拉动和对象引用时,可能需要一段时间。
dim temparr as new arraylist()
While dr.Read()
temparr.add(dr.item("field_name"))
End While
If temparr.count = 0 Then
Field_nameComboBox.Text = ""
Field_valueComboBox.Items.Clear()
lbl_fld_fieldv.Text = "0"
Sort_priorityLabel1.Visible = False
lbl_field_sortprio.Visible = False
Field_valueComboBox.Visible = False
lbl_field_fvalues.Visible = False
else
lbl_field_fldno.Text = temparr.count
for each l_item as string in temparr
txtbx_field_list.Text += l_item
Field_nameComboBox.Items.Add(l_item)
next
End If