Results 1 to 10 of 10

Thread: Retrieving Data

  1. #1

    Thread Starter
    Addicted Member waelr's Avatar
    Join Date
    Nov 2004
    Posts
    168

    Retrieving Data

    to retrieve data im using this code:

    SqlConnection1.Open()
    Dim x As SqlClient.SqlDataReader
    Dim s
    SqlCommand1.CommandText = "Select sum(I_AMOUNT) from INVOICES where I_YEAR = '" & ComboBox6.Text & "'"
    x = SqlCommand1.ExecuteReader()
    While (x.Read())
    s = x.GetValue(0)
    End While
    x.Close()
    TextBox1.Text = s
    SqlConnection1.Close()

    this code allows me to retrieve only one row of data
    what should i do in order to retrieve many rows and display them in a datagrid
    is there something to use instead of x.getvalue(0)

  2. #2
    Lively Member jpgonc's Avatar
    Join Date
    May 2005
    Posts
    75

    Re: Retrieving Data

    I don't know if this works...

    Put a breakpoint on while and see if it loops more than one time.
    If it does, put a Count = Count+1 before the GetValue and then change s = x.GetValue(0), to s = x.GetValue(count)

    NOT TESTED! See for yourself.
    ----------------------------------------------------
    Centaurs were mythological creatures like chimeras and satyrs and they had a tendency to violence, except Chiron, who taught Greek heroes.
    ----------------------------------------------------

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Retrieving Data

    It probably does get more than one row of data but you're only taking a value from the very last row because you are overwriting the contents of the "s" variable each time through the loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Lively Member jpgonc's Avatar
    Join Date
    May 2005
    Posts
    75

    Re: Retrieving Data

    Try this...

    VB Code:
    1. SqlConnection1.Open()
    2. Dim x As SqlClient.SqlDataReader
    3. Dim s
    4. SqlCommand1.CommandText = "Select sum(I_AMOUNT) from INVOICES where I_YEAR = '" & ComboBox6.Text & "'"
    5. x = SqlCommand1.ExecuteReader()
    6. While (x.Read())
    7. count=count+1
    8. s = x.GetValue(count)
    9. textbox1.text=s ' Set Multiline=TRUE in the object properties.... or make an array
    10. End While
    11. x.Close()
    12. SqlConnection1.Close()
    ----------------------------------------------------
    Centaurs were mythological creatures like chimeras and satyrs and they had a tendency to violence, except Chiron, who taught Greek heroes.
    ----------------------------------------------------

  5. #5

    Thread Starter
    Addicted Member waelr's Avatar
    Join Date
    Nov 2004
    Posts
    168

    Re: Retrieving Data

    I need to display my data in a DataGrid

  6. #6
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Retrieving Data

    waelr its normal it only return one row your using the aggregate function SUM .

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  7. #7
    Lively Member jpgonc's Avatar
    Join Date
    May 2005
    Posts
    75

    Re: Retrieving Data

    LOL Asgorath, didn't noticed that...

    Waelr, run your SQL and see how many records you get...lol.
    ----------------------------------------------------
    Centaurs were mythological creatures like chimeras and satyrs and they had a tendency to violence, except Chiron, who taught Greek heroes.
    ----------------------------------------------------

  8. #8
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Retrieving Data

    Once you modified your sql query just use a SqlDataAdapter1 to fill a dataset and assign it to your datagrid datasouce property.
    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  9. #9

    Thread Starter
    Addicted Member waelr's Avatar
    Join Date
    Nov 2004
    Posts
    168

    Re: Retrieving Data

    LOL
    i copied and pasted the code without reading
    forget about the sum
    the problem persists even if i select something else
    i tried assignining a datasource to the datagrid after filling the dataset but i get a problem with a "+" sign on the top left of the grid which i need to press on to display the results

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Retrieving Data

    Set the datasource of the datagrid to a particular table in the dataset, like so:

    DataGrid1.DataSource = ds.Tables(0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width