现在vb.net selctedindexchanged事件中有什么 [英] what is in vb.net selctedindexchanged event now

查看:73
本文介绍了现在vb.net selctedindexchanged事件中有什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,我首先使用了以下代码

dear sir read the following code I''m using first

dim con1 as new MyCon
dim Tbl as string= "StudentBasic"
dim Cmd as string = "SELECT SNo, SerialNo, studentName FROM StudentBasic"
Cmd &= " Where SerialNo = " & Val(CmbSerial.Text)

Con1.NewCon()
Con1.NewDC(Cmd, Tbl)

Dim rcount = Con1.DS.Tables(Tbl).Rows.Count


Dim Drow As DataRow = Con1.DS.Tables(Tbl).Rows(0)
LblName.Text = Drow("studentName")

Con1.closeCon(Tbl)


在上面的代码中,我只是通过类Mycon
中的DataAdaptor从数据库中打开表
我的表具有序列号作为主键.

一切都很好.

当我在VB Form的加载事件中使用此代码时,我从表中的rcount中得到一条记录
但是
当我在组合框的SelectedIndexChangedTextChange事件中使用它时,它在rcount中给出2条记录.

为什么?


in the above code I''m just opening my table from database through DataAdaptor from my class Mycon

My Table has Serial No as Primary key.

All is well.

When I use this code in load event of VB Form then I get one record in my rcount from the table
BUT
when I use it in SelectedIndexChanged or TextChange event of combo box then it give 2 record in rcount.

WHY?

推荐答案

您尚未显示MyCon类中的任何代码,该代码是所有操作发生的地方,但从您的描述中听起来像是如果数据两次被加载到表(tbl)中,并且在填充之前没有被清除.

MyCon使用它的DataAdapter.Fill()方法的地方首先清除旧数据:
You have not shown any of the code from your MyCon class, which is where all the action takes place, but from your description it sounds as if the data is being loaded into the table (tbl) twice and is not being cleared before the fill.

Where MyCon uses it''s DataAdapter.Fill() method clear the old data first:
ds.Tables("Bacon Sandwich").Clear()
da.Fill(ds, "Bacon Sandwich")


感谢回复
首先,我的数据库表对每个序列号都有唯一的记录.再次,以上代码在负载或按钮单击事件中工作正常.因此,为什么它在组合,文本更改或索引更改事件中无法正常工作.它始终为每个唯一的序列号记录2条记录...第一个为空,第二个为真实数据.

以下是我的Mycon课程,以获取更多许可

导入Microsoft.VisualBasic
导入System.Data
导入System.Data.OleDb
导入System.IO

公共类Mycon
Dim conn As OleDb.OleDbConnection
昏暗的DT作为新的数据表
公开DS作为新数据集
昏暗的DA作为新的OleDb.OleDbDataAdapter
Dim DC As OleDb.OleDbCommand

Public Sub NewCon()
conn =新的OleDb.OleDbConnection()
昏暗的DPath作为字符串= Application.StartupPath& "\ SSMandir1.mdb"

conn.ConnectionString ="PROVIDER = microsoft.Jet.OLEDB.4.0;数据源=& DPath

DA.MissingSchemaAction = MissingSchemaAction.AddWithKey
conn.Open()
结束子

Public Sub NewDC(ByRef cmd作为字符串,ByVal tbl作为字符串)
DC =新的OleDb.OleDbCommand(cmd,conn)
DA =新的OleDb.OleDbDataAdapter(DC)
DA.Fill(DS,tbl)
结束子

Public Sub closeCon(ByRef st As String)
conn.Close()
conn.Dispose()
结束子

Public Sub UpdateTbl(ByRef tbl As String)

昏暗的CB =新的OleDbCommandBuilder(DA)
CB.ConflictOption = ConflictOption.OverwriteChanges''这将使具有不同数据类型输入条件的列中的更改无效.像以前一样是数字,现在您将其替换为字符串
DA.Update(DS,tbl)
结束子


结束类

现在解决我的问题.我做了很多次,但是第一次遇到这个问题!!!!!!
thanks for reply
first of all my database table has unique record for each serial number. and again, above code is working fine in load or button click event. so why its not working well in combo, text changed or index change event. it always count 2 record for each unique serial number ..... first is null and second is real data.

for more clearance below is my Mycon class

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb
Imports System.IO

Public Class Mycon
Dim conn As OleDb.OleDbConnection
Dim DT As New DataTable
Public DS As New DataSet
Dim DA As New OleDb.OleDbDataAdapter
Dim DC As OleDb.OleDbCommand

Public Sub NewCon()
conn = New OleDb.OleDbConnection()
Dim DPath As String = Application.StartupPath & "\SSMandir1.mdb"

conn.ConnectionString = "PROVIDER = microsoft.Jet.OLEDB.4.0 ; Data Source =" & DPath

DA.MissingSchemaAction = MissingSchemaAction.AddWithKey
conn.Open()
End Sub

Public Sub NewDC(ByRef cmd As String, ByVal tbl As String)
DC = New OleDb.OleDbCommand(cmd, conn)
DA = New OleDb.OleDbDataAdapter(DC)
DA.Fill(DS, tbl)
End Sub

Public Sub closeCon(ByRef st As String)
conn.Close()
conn.Dispose()
End Sub

Public Sub UpdateTbl(ByRef tbl As String)

Dim CB = New OleDbCommandBuilder(DA)
CB.ConflictOption = ConflictOption.OverwriteChanges '' this will aloow the changes in columns with differ datatype entry condition. like previous is number and now u r replacing it with a string
DA.Update(DS, tbl)
End Sub


End Class

Now sort my problem. i did it many time but facing this problem first time !!!!!!


这篇关于现在vb.net selctedindexchanged事件中有什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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