Doubt about retrieving data from database using listbox values

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2792 Views - Last Post: 24 March 2012 - 09:23 PM Rate Topic: -----

#1 babbu25@gmail.com  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-November 11

Doubt about retrieving data from database using listbox values

Posted 20 November 2011 - 06:10 AM

Hi friends.. I am doing an vb.net airlines project for my school.I have a list box In which i am retrieving flightname details from the database..
 Dim cnString As String

        cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=AirLine.mdb"

        Dim sqlQRY As String = "SELECT flightname FROM FlightDetails"

        'Create connection
        Dim conn As OleDbConnection = New OleDbConnection(cnString)

        Try
            ' Open connection
            conn.Open()

            ' txtStatus.Text = txtStatus.Text & "Connection opened" & vbCrLf & vbCrLf

            'create data adapter
            Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlQRY, conn)

            'create dataset
            Dim ds As DataSet = New DataSet

            'fill dataset
            da.Fill(ds, "FlightDetails")
            'get data table
            Dim dt As DataTable = New DataTable()
            Dim d As DataColumn

            dt = ds.Tables("FlightDetails")
            'd = dt.Columns("flightId")
            For i As Integer = 0 To dt.Rows.Count - 1
                ListBox1.Items.Add(dt.Rows(i).Item("flightname"))

            Next
            da.Dispose()
        Finally
            ' Close connection
            conn.Close()

        End Try
    End Sub



Well now i have to select a particular name in listbox and the details of that flightname has to come from database into the datafields whic i have n the same form .

Questions

1)what i m trying is for an ListBox1_SelectedIndexChanged I am trying to connect and when ever i change the index i am connecting to database and retrieving values..DO u think this is a best idea. :online2long:

2) For My query can i have n such a way that when ever i select any field in listbox based on that i have to retrieve few fields from the database.I am little bit confused.ANy help is appreciable .. :whistling:


Thank u :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Doubt about retrieving data from database using listbox values

#2 babbu25@gmail.com  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-November 11

Re: Doubt about retrieving data from database using listbox values

Posted 20 November 2011 - 10:13 AM

Can any one answer please...i am waiting for my answer..I have to work based on the answer .. :stuart:

Thank You .
Was This Post Helpful? 0
  • +
  • -

#3 kingsonprisonic  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 09-July 09

Re: Doubt about retrieving data from database using listbox values

Posted 20 November 2011 - 02:39 PM

Yes u can use listbox's listindex change event. And retrieve the data from database by filtering it ..


"select * from flightdetails where someid="+me.Listbox1.SelectedItem

Now retrive it and show it...


Am i clear????
Was This Post Helpful? 0
  • +
  • -

#4 babbu25@gmail.com  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-November 11

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 06:00 PM

View Postkingsonprisonic, on 20 November 2011 - 01:39 PM, said:

Yes u can use listbox's listindex change event. And retrieve the data from database by filtering it ..


"select * from flightdetails where someid="+me.Listbox1.SelectedItem

Now retrive it and show it...


Am i clear????




Hmmm.thanks mate..but i am getting an error when i keep this .



da.Fill(ds, "FlightDetails")-------------------at this point error



At this point i am getting the following error...
No value given for one or more required parameters.
Any solutions ???
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 06:10 PM

Can we see your updated code please?
Was This Post Helpful? 0
  • +
  • -

#6 babbu25@gmail.com  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-November 11

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 06:33 PM

 cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=AirLine.mdb"
        Dim s As String = ""
        s = Me.ListBox1.SelectedItem

        Dim sqlQRY As String = "SELECT flightid FROM FlightDetails WHERE flightname='s'"

        'Create connection
        Dim conn As OleDbConnection = New OleDbConnection(cnString)

        Try
            ' Open connection
            conn.Open()
            'create data adapter
            Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlQRY, conn)

            'create dataset
            Dim ds As DataSet = New DataSet

            'fill dataset
            da.Fill(ds, "FlightDetails")
            'get data table
            Dim dt As DataTable = New DataTable()
            Dim d As DataRow

            dt = ds.Tables("FlightDetails")
            

            For i As Integer = 0 To dt.Rows.Count - 1
                d = dt.Rows(0)
                txtFrm.Text = d("flightid")

                ' d("flightId")
                '' ListBox1.Items.Add(dt.Rows(i).Item("flightname"))
            Next

            da.Dispose()
        Finally
            ' Close connection
            conn.Close()

        End Try



Here is my edited code.I am still getting no value ..

When i debug the program its saying there is no row(0) value ..please help me .
Was This Post Helpful? 0
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 06:56 PM

View Postbabbu25@gmail.com, on 22 November 2011 - 11:33 AM, said:

 cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=AirLine.mdb"
        Dim s As String = ""
        s = Me.ListBox1.SelectedItem

        Dim sqlQRY As String = "SELECT flightid FROM FlightDetails WHERE flightname='s'"

        'Create connection
        Dim conn As OleDbConnection = New OleDbConnection(cnString)

        Try
            ' Open connection
            conn.Open()
            'create data adapter
            Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlQRY, conn)

            'create dataset
            Dim ds As DataSet = New DataSet

            'fill dataset
            da.Fill(ds, "FlightDetails")
            'get data table
            Dim dt As DataTable = New DataTable()
            Dim d As DataRow

            dt = ds.Tables("FlightDetails")
            

            For i As Integer = 0 To dt.Rows.Count - 1
                d = dt.Rows(0)
                txtFrm.Text = d("flightid")

                ' d("flightId")
                '' ListBox1.Items.Add(dt.Rows(i).Item("flightname"))
            Next

            da.Dispose()
        Finally
            ' Close connection
            conn.Close()

        End Try



Here is my edited code.I am still getting no value ..

When i debug the program its saying there is no row(0) value ..please help me .

I think that if you are using a For loop then instead of d = dt.Rows(0) it would be this: d = dt.Rows(i)
Was This Post Helpful? 0
  • +
  • -

#8 babbu25@gmail.com  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-November 11

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 07:18 PM

Nope..The problem is that its not at all going into the loop..the value
Rows.count is always 0...thats the problem ....@jimmybo.any suggestions?
Was This Post Helpful? 0
  • +
  • -

#9 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 07:24 PM

Have you tried jimmyBo's suggestion?
Just try it first then tell us what error do you encounter.

This post has been edited by Elda: 21 November 2011 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#10 babbu25@gmail.com  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-November 11

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 08:07 PM

.@@Elda

I am getting an error telling that there is no row value 0
Was This Post Helpful? 0
  • +
  • -

#11 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 08:28 PM

Here just try


 

                d = dt.Rows(i)

                txtFrm.Text = t.Rows(i)("flightid").ToString()

           

Was This Post Helpful? 0
  • +
  • -

#12 babbu25@gmail.com  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-November 11

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 08:53 PM

View PostElda, on 21 November 2011 - 07:28 PM, said:

Here just try


 

                d = dt.Rows(i)

                txtFrm.Text = t.Rows(i)("flightid").ToString()

           



NOpe i tried its showing the same error ... "THERE IS NO ROW AT POSITION 0"..
Was This Post Helpful? 0
  • +
  • -

#13 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: Doubt about retrieving data from database using listbox values

Posted 21 November 2011 - 09:15 PM

I tried your codes and it works.
Try to double check your table if it has data.

This post has been edited by Elda: 21 November 2011 - 09:17 PM

Was This Post Helpful? 0
  • +
  • -

#14 babbu25@gmail.com  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-November 11

Re: Doubt about retrieving data from database using listbox values

Posted 22 November 2011 - 07:14 AM

@elda..i have tried so much but i am geting same error..

i have small doubt in following code
 Dim s As String = ""
        s = Me.ListBox1.SelectedItem
        Dim sqlQRY As String = "SELECT flightid FROM FlightDetails WHERE flightname='s'"



Are we really taking the listbox selected item and sending in a SQL query.Is this possible.I got this doubt because if replace flightname='s' with flightnamw=SOME VALUE.. I am able to retrieve that value.

I think there is some other way to check data in my SQL Query,ANy ideas??
Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Doubt about retrieving data from database using listbox values

Posted 22 November 2011 - 07:26 AM

Dim sqlQRY As String = "SELECT flightid FROM FlightDetails WHERE flightname='s'"

This is funny, s is a variable, you cant treat it as a string like that, you have to say
Dim sqlQRY As String = "SELECT flightid FROM FlightDetails WHERE flightname='" & s & "'"

Or use parameters, which is a good way to avoid sql injection

This post has been edited by smohd: 22 November 2011 - 07:27 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2