3 Replies - 9226 Views - Last Post: 14 April 2010 - 06:41 AM Rate Topic: -----

#1 mihaispr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 29-September 09

Search rows from database and store results in listview vb.net

Posted 15 November 2009 - 09:30 AM

Hi to the community of dreamincode.net!

I have a little problem with interacting with sql server from vb.net.


I look here and read this tutorial from your forum:

http://www.dreaminco...wtopic32392.htm

And now I shall detail my problem.


I made an sql server table called Per:

Posted Image

Until here all it's ok.


What I want to do is to insert in the two textboxes the user types Mike (in textbox1) and Bottomley (in textbox2) ,press the button search and stored the results in listview if he finds the row(and in this case it finds).

Here is the printscreen of my windows forms application vb.net(i'm using visual studio 2008 ):

Posted Image

So I type in the two textboxes Mike(textbox1) , Bottomley(textbox2) press the button and it searches in my sql server table (if it finds -in this case I have this rows it's row1) it displays me the result in the Listview.

That's my problem.

Any and all help is appreciated!

This post has been edited by mihaispr: 15 November 2009 - 09:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Search rows from database and store results in listview vb.net

#2 Donnie1581  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 05-November 09

Re: Search rows from database and store results in listview vb.net

Posted 15 November 2009 - 10:26 AM

Show us your Button Click Event code
Was This Post Helpful? 0
  • +
  • -

#3 mihaispr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 29-September 09

Re: Search rows from database and store results in listview vb.net

Posted 15 November 2009 - 12:06 PM

View PostDonnie1581, on 15 Nov, 2009 - 09:26 AM, said:

Show us your Button Click Event code


My database is called test and my table is called Per.

Private Sub Form_Load()

Set MyDatabase = OpenDatabase("test.sql")
Set MyTable = MyDatabase.OpenRecordSet("Per", dbOpenDynaset)



In the button that I have on my form:


Private Sub Button1_Click()

'variable con is for connection is gived by server name , username=user and password =pwd

Dim con As New SqlConnection("HOME-PRIMAVERA";user=mihaispr;pwd=mihai")
		Dim cmd As New SqlCommand("Select * From Per", con)
		con.Open()
	   LisView1.Add(cmd)
		con.Close()

'otherwise if any row isn't search in textbox and it doesn't lie in table call errordialog
if con=" "
ErrorDialog.ShowDialog()
ShowMessage('Can't connect to database')

if cmd= " "
ErroDialog.ShowDialog()
ShowMessage('The  table doesn't contain the rows specified by values of the 2 textboxes')
'also i want to display an error message if any rows isn't in my sql server table
End Sub



Any ideas or it's a simple solution for this?

This post has been edited by mihaispr: 15 November 2009 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_marilynjane*


Reputation:

Re: Search rows from database and store results in listview vb.net

Posted 14 April 2010 - 06:41 AM

I am using oledb, but perhaps this will help - it works for me... perhaps put this (not the oledb version) between con.Open and con.Close...

Dim da As OleDb.OleDbDataAdapter
Dim dt As New DataTable
da.SelectCommand.CommandText = "SELECT * FROM dbo.YourTable WHERE FirstName='" & TextBox1.Text & "'"
da.Fill(dt)
ListView1.Add(dt.Rows(0)("FirstName"))
dt.Reset()
Was This Post Helpful? 0

Page 1 of 1