2 Replies - 4611 Views - Last Post: 16 June 2013 - 05:00 PM Rate Topic: -----

#1 mcavanilla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-June 13

Displaying result of SQL search in textbox

Posted 14 June 2013 - 10:04 AM

Hello,

Could someone help me to display the result of a search in textbox?


I have a table ("TBClient"), with two columns "Company" and "Name". My form has a combox to select the name of a column, and a textbox to write what i'm looking for. Below this, i have a textbox to display the result.
How can I search for a company and display the result (the name of the company), into this texbox.
I'm using VS2010, SQL server 2008 and DataSet

My code:

Private Sub btnPesquisa1_Click(sender As Object, e As System.EventArgs) Handles btnPesquisa1.Click

Dim data As SqlDataAdapter
Dim sql As String
Dim dt As New DataSet


If cmbNames.SelectedIndex = "0" Then
sql = "Select * from TBClient where Company='" & txtpesquisa1.Text & "'"
End If


data = New SqlDataAdapter(sql, cn)
data.Fill(dt, "TBCliente")
Dim dr As DataRow = dt.Tables(0).Rows(0)


DataGrid2.DataSource = dt.Tables(0)


TextBox1.Text = dr("Company").ToString 



Thanks for help

Marco

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying result of SQL search in textbox

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,457
  • Joined: 12-December 12

Re: Displaying result of SQL search in textbox

Posted 14 June 2013 - 10:26 AM

If cmbNames.SelectedIndex = "0" Then

SelectedIndex is an Integer so you should be comparing it to 0. I recommend that you add Option Strict On to the top of your code, which will indicate these types of errors.

ComboBox: MSDN

sql = "Select * from TBClient where Company='" & txtpesquisa1.Text & "'"

You can just use an ElseIf clause to alternatively search for "Name". If there were more than two columns then it would be preferable to substitute the text from the combobox for Company in your sql statement. (This is preferable anyway, in case the combobox items appear in a different order.)

If using a TextBox you would need to use & or String.Concat to populate it, also manually inserting line-breaks. A ListBox would be preferable.

Once you've got it working to find the full Company, or Name, you might investigate the sql Like operator to a perform a partial search. That is, the user would only need to enter part of the Company or Name.

This post has been edited by andrewsw: 14 June 2013 - 10:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 progammerbeat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-April 13

Re: Displaying result of SQL search in textbox

Posted 16 June 2013 - 05:00 PM

Like a text box for use string
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1