Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading data fromSQL server into a data??? something 2

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I don't want to drag something on my form I want to open a recordset programmatically and move back and forth through it. I would like to make a connection to a SQL database on my WEB site, then open a table or recordset with a select statement then be able to movefirst or movenext through that recordset and also be able to extract the columns data textbox = somefieldname.
I seem to get a lot of code written only to find out a data reader can't movelast or movenext or this can't use a field name or something. driving me nuts!


here is my vs2005 code so far
Code:
        Dim conn As SqlConnection
        Dim da As SqlDataAdapter = New SqlDataAdapter()
        Dim cmd As SqlCommand

        conn = New SqlConnection
        conn.ConnectionString = "server=000.000.000.000;uid=user;pwd=pass;database=myDB"
        cmd = New SqlCommand("Select * from [TimeSheet Employees]", conn)
        da.SelectCommand = cmd
        Dim custDS As DataSet = New DataSet
        da.Fill(custDS, "Select * from [TimeSheet Employees]")
        'move to first record
        'or move to last record
        'or read all records one at a time
        'be able to move back to first record 
         me.txtName = da?????  ("Name")
        me.txtAddress = da????? ( "Address")
       'etc
        conn = Nothing
        da = Nothing
        cmd = Nothing

DougP
[r2d2] < I Built one
 
You can kind of MoveNext with a DataReader. But to have control like you want, use a DataTable. Here's a crude example to demonstrate this. Create a Windows Form project, create a TextBox called TextBox1 and a button called btnMoveNext. Change the connection string and try it out. The same data principles would apply to Web Forms as well.

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As New SqlClient.SqlConnection("Data Source=SomeServer;Initial Catalog=SomeDatabase;Integrated Security=SSPI;")
        Dim da As New SqlClient.SqlDataAdapter
        Dim cmd As New SqlClient.SqlCommand
        Dim dt As New DataTable
        dt.TableName = "COLUMNS"
        With cmd
            .Connection = con
            .CommandType = CommandType.Text
            .CommandText = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS"
        End With
        With da
            .SelectCommand = cmd
            .Fill(dt)
        End With

        Me.TextBox1.DataBindings.Add("Text", dt, "COLUMN_NAME")
    End Sub

    Private Sub btnMoveNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveNext.Click
        Dim cm As CurrencyManager = CType(Me.BindingContext(Me.TextBox1.DataBindings(0).DataSource), CurrencyManager)
        Try
            cm.Position += 1
        Catch ex As Exception

        End Try
    End Sub
 
I forgot to mention--here is some code for "Move Last"

Code:
    Private Sub btnMoveLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        Dim cm As CurrencyManager = CType(Me.BindingContext(Me.TextBox1.DataBindings(0).DataSource), CurrencyManager)
        Try
            cm.Position = CType(Me.TextBox1.DataBindings(0).DataSource, DataTable).Rows.Count - 1
        Catch ex As Exception

        End Try
    End Sub
 
and I need to movefirst
I have a recordset I am moving through and want to move it back to the top and movenext though it again.

also what is CurrencyManager?
Dim cm As CurrencyManager = CT


DougP
[r2d2] < I Built one
 
A data reader can move only forwards through the records:

Do While dr.Read
txtbox.value = dr("Surname")
'etc
Loop
 
RiverGuys example uses a dataTABLE
I don't want a datareadeer for that reason
Dim dt As New DataTable

plus I said that in the first post
I seem to get a lot of code written only to find out a data reader can't movelast or movenext or

and Is there anything like th old ADO control in VB6 which did movefirst, movelast, next, and prev?

DougP
[r2d2] < I Built one
 
and I need to movefirst
cm.Position = 0 would move first.

also what is CurrencyManager?
CurrencyManager definition:

and Is there anything like th old ADO control in VB6 which did movefirst, movelast, next, and prev?
I don't know. I rarely used the ADO control, and have never looked into using an equivalent in .Net.
 
Fanatastic RiverGuy,

I gave your 3 stars since I was seeing so many stars trying to figure it out and trying to get help for this for a LONG LONG LONG LONG time

DougP
[r2d2] < I Built one
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top