Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i start Project and click on search button every things ok
but when click once again
this error is show (This causes two bindings in the collection to bind to the same property)

this my code
VB.NET
  1  Imports System.Data.SQLite
  2  
  3  Public Class Form1
  4  
  5      Private dbcomand As String = ""
  6      Private bindingSrc As BindingSource
  7  
  8      Private dbName As String = "conquest.db3;"
  9      Private dbPath As String = "D:\conquestdicomserver\data\dbase\" & dbName
 10      Private conString As String = "Data Source=" & dbPath & "Version=3;New=False;Compress=True;"
 11  
 12      Private connection As New SQLiteConnection(conString)
 13      Private command As New SQLiteCommand("", connection)
 14  
 15      Private sql As String = ""
 16  
 17      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 18  
 19          connection.Open()
 20  
 21          'UpdateDatabiding()
 22  
 23          connection.Close()
 24  
 25  
 26      End Sub
 27  
 28      Private Sub UpdateDatabiding(Optional cmd As SQLiteCommand = Nothing)
 29          ''TODO
 30          Try
 31              If cmd Is Nothing Then
 32                  command.CommandText = "SELECT StudyDate,PatientNam,StudyModal,StudyDescr,PatientsAg,PatientID FROM DICOMStudies"
 33              Else
 34                  command = cmd
 35              End If
 36  
 37              Dim adapter As New SQLiteDataAdapter(command)
 38              Dim dataSt As New DataSet()
 39              adapter.Fill(dataSt, "DICOMStudiesList")
 40  
 41              bindingSrc = New BindingSource()
 42              bindingSrc.DataSource = dataSt.Tables("DICOMStudiesList")
 43  
 44              Dim tb As TextBox
 45              For Each ctr As Control In GroupBox1.Controls
 46                  If TypeOf ctr Is TextBox Then
 47                      tb = CType(ctr, TextBox)
 48                      tb.DataBindings.Clear()
 49                      tb.Text = ""
 50                  End If
 51  
 52              Next
 53  
 54  
 55              IDShowTextBox.DataBindings.Add("Text", bindingSrc, "PatientID")
 56              NameShowTextBox.DataBindings.Add("Text", bindingSrc, "PatientNam")
 57  
 58              DataGridView1.Enabled = True
 59  
 60              DataGridView1.DataSource = bindingSrc
 61  
 62              DataGridView1.AutoResizeColumns(CType(DataGridViewAutoSizeColumnsMode.AllCells, DataGridViewAutoSizeColumnsMode))
 63              DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
 64  
 65              DataGridView1.Columns(0).Width = 60
 66              DisplayPosition()
 67  
 68          Catch ex As Exception
 69              MessageBox.Show("Data Binding Error:0 " & ex.Message.ToString())
 70          End Try
 71  
 72  
 73      End Sub
 74  
 75      Private Sub DisplayPosition()
 76          PositionLabel1.Text = "Position:  " & bindingSrc.Position + 1 & "/" & bindingSrc.Count
 77      End Sub
 78  
 79      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 80  
 81          Dim Psi As New ProcessStartInfo
 82          With Psi
 83              .FileName = "C:\Program Files\RadiAntViewer64bit\RadiAntViewer.exe"
 84              .Arguments = "D:\conquestdicomserver\data\" & IDShowTextBox.Text
 85          End With
 86          Process.Start(Psi)
 87  
 88      End Sub
 89  
 90      Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
 91  
 92          If connection.State = ConnectionState.Closed Then
 93              connection.Open()
 94          End If
 95  
 96          Try
 97              If String.IsNullOrEmpty(KeywordTextBox.Text.Trim()) Then
 98                  UpdateDatabiding()
 99                  Exit Sub
100              End If
101  
102              sql = "SELECT StudyDate,PatientNam,StudyModal,StudyDescr,PatientsAg,PatientID FROM DICOMStudies WHERE PatientNam LIKE @keyword2"
103              ' sql &= "WHERE PatientNam LIKE @keyword2 "
104  
105  
106              command.CommandType = CommandType.Text
107              command.CommandText = sql
108  
109              command.Parameters.Clear()
110  
111              Dim KeywordString As String = String.Format("%{0}%", KeywordTextBox.Text)
112  
113              command.Parameters.AddWithValue("@keyword1", KeywordTextBox.Text)
114              command.Parameters.AddWithValue("@keyword2", KeywordString)
115  
116              UpdateDatabiding(command)
117  
118  
119          Catch ex As Exception
120              MessageBox.Show("Search Error:1 " & ex.Message.ToString(),
121                              "Error Message : error Tutorial.",
122                              MessageBoxButtons.OK, MessageBoxIcon.Error)
123          Finally
124              connection.Close()
125              KeywordTextBox.Focus()
126          End Try
127  
128      End Sub
129  End Class


What I have tried:

I need help
when i start Project and click on search button every things ok
but when click once again
this error is show (This causes two bindings in the collection to bind to the same property)
Posted
Updated 30-Jan-23 2:40am
v2
Comments
Member 15627495 30-Jan-23 4:47am    
you have to 'clear/empty' your dataset, and your 'display components' , before refill them

1 solution

Why do you add databinding to the controls in UpdateDatabiding method?
VB.NET
IDShowTextBox.DataBindings.Add("Text", bindingSrc, "PatientID")
NameShowTextBox.DataBindings.Add("Text", bindingSrc, "PatientNam")


You need to do that only once. :)

I'd suggest to read this:
Advanced Basics: Data Binding in Visual Basic .NET | Microsoft Learn[^]
Data Binding Concepts in .NET Windows Forms[^]
 
Share this answer
 

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