Click here to Skip to main content
15,881,719 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear sir read the following code I'm using first
VB
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)

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?
Posted
Updated 9-Apr-11 9:44am
v3

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:
VB
ds.Tables("Bacon Sandwich").Clear()
da.Fill(ds, "Bacon Sandwich")
 
Share this answer
 
Comments
Sandeep Mewara 10-Apr-11 3:12am    
OP posted whole lot of thing (comment + code) for you but as an answer. PLease have a look.
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 !!!!!!
 
Share this answer
 
Comments
Henry Minute 10-Apr-11 6:29am    
Did you try
Public Sub NewDC(ByRef cmd As String, ByVal tbl As String)
DC = New OleDb.OleDbCommand(cmd, conn)
DA = New OleDb.OleDbDataAdapter(DC)
DS.Tables(tbl).Clear()
DA.Fill(DS, tbl)
End Sub

as I suggested previously?
onlineQry 11-Apr-11 5:17am    
yes but it give DS not defined error. as
Object reference not set to an instance of an object. on this line
Henry Minute 11-Apr-11 7:08am    
Sorry, I should have spotted the possibility that this might happen. You should test for that table existing before trying to clear it..

Some variation on:
if (DS.Tables.Contains(tbl))
{
DS.Tables(tbl).Clear()
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900