Moonshadow

I am creating an application that provides a full-text search on a db. The full-text search works fine and loads some brief descriptive data into DataGridView1. The user then selects a row and the SectionText column of text is loaded into DisplayTextBox. I make two calls to the db to speed it up. The SectionText column can be several pages long and slows the search down significantly so I bring back only what is need on the second call. At this point I need help with two problems:

Problem #1. Transferring string data from the DataGridView1 (Record1 & Record2). If I put these variables into Textbox3 and TextBox5 and then use that in the WHERE clause, it works but is clumsly. I would prefer to work directly with these variables if I can get the syntax right.

Problem #2: Although DataGridView1 and DisplayTextBox fill with the appropriate data as a result of the search, when I select a row in DataGridview1 I get this error message:

DisplayTextBox.DataBindings.Add("Text", ds.Tables(0), "SectionText")
ARGUMENTEXCEPTION WAS UNHANDLED. This causes two bindings in the collection to bind to the same property. Parameter name: binding

Any help in sorting out these two issues would be greatly appreciated. My code:

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim strSearch
strSearch = TextBox1.Text
Dim conn As New SqlConnection("XXX.)
Dim ds As New DataSet
conn.Open()
Dim adp As New SqlDataAdapter("Select FullDocNo, DocType, From FullDocuments WHERE CONTAINS(SectionText, ' """ & strSearch & """ ')", conn)
adp.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
conn.Close()
End Sub

Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged

If Loading Or DataGridView1.SelectedCells.Count = 0 Then Exit Sub

Dim cell As DataGridViewCell = _
DirectCast(sender, DataGridView).SelectedCells.Item(0)
Dim rowindex As Integer = cell.RowIndex
Dim Record1 As String = DataGridView1.Rows(rowindex).Cells.Item(0).Value
Dim Record2 As String = DataGridView1.Rows(rowindex).Cells.Item(1).Value

'WOULD LIKE TO BE ABLE TO ELIMINATE THESE!
TextBox3.Text = Record1.ToString
TextBox5.Text = Record2.ToString

Dim conn As New SqlConnection("xxx)
Dim ds As New DataSet
conn.Open()

Dim adp As New SqlDataAdapter("SELECT SectionText FROM FullDocuments WHERE FullDocNo LIKE '" & TextBox3.Text & "' AND DocType LIKE '" & TextBox5.Text & "'", conn)

'CAN'T GET THIS VERSION TO WORK!
'Dim adp As New SqlDataAdapter("SELECT SectionText FROM FullDocuments WHERE FullDocNo LIKE 'Record1' AND DocType LIKE 'Record2'", conn)

adp.Fill(ds)
DisplayTextBox.DataBindings.Add("Text", ds.Tables(0), "SectionText")
conn.Close()

End Sub

End Class



Re: Windows Forms Data Controls and Databinding Need Help with Variables and Databinding

Moonshadow

OK. Clearing the Textbox each time before databinding it fixed Problem #2.

DisplayTextBox.DataBindings.Clear()
DisplayTextBox.DataBindings.Add("Text", ds2.Tables(0), "SectionText")

Any suggestion on Problem #1 (using variables in the Select statement)




Re: Windows Forms Data Controls and Databinding Need Help with Variables and Databinding

Moonshadow

Seems I can use the same syntax for the variables as I used with the textbox.text. Thought I had tried that, but must have overlooked it. This works:

Dim adp As New SqlDataAdapter("SELECT SectionText FROM FullDocuments WHERE FullDocNo LIKE '" & Record1 & "' AND DocType LIKE '" & Record2 & "'", conn)