Results 1 to 5 of 5

Thread: DataReader...HELP!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    DataReader...HELP!

    I am trying to access data in SQL Server and pull it into the form. I am somewhat new at this. Can anyone help me out? I have one form open and you type in the case number. or lastname. Then I want it to be able to pull in data into another form. PLEASE HELP! This is what I have so far:

    VB Code:
    1. SqlConn.Open()
    2.  
    3.         Dim drSqlDataReader1 As SqlDataReader = cmdSqlCommand1.ExecuteReader()
    4.         drSqlDataReader1.Read()
    5.          Dim search As String
    6.  
    7.         If IsNumeric(txtSearchCase.Text) Then
    8.             search = "SELECT * FROM CapRec WHERE CaseNumber LIKE '%" & txtSearchCase.Text & "%';"
    9.  
    10.             'txtCaseNum.Text = drSqlDataReader1.Item(0)
    11.  
    12.         Else
    13.             search = "SELECT * FROM CapRec WHERE LastName LIKE '%" & txtSearchCase.Text & "%';"
    14.         End If

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Step one is to define a Connection object.
    Step two is to define a Command object.
    Step three is to define a Reader object.
    Step four is to call the Open method of the Connection object
    Step five is to call ExecuteReader on the Command object, and assigning its output to the Reader object.
    Step six is to keep reading the datareader pipe until it is exhausted
    Step seven, close the connection.

    With a reader object, like SqlDataReader, each time you call the Read() method, the reader advances one record in its pipe of records, and assigns that record as the current record available to retrieve data from.

    So after calling Read(), you can get a value out of the current assigned record, by using GetValue. But I never use GetValue for the most part, because the datareader class has a method that allows you use shorthand to retrieve the data in the column you specify ( for example: me.text = dr(0) ).

    In closing, when the Read method return false, its had read all the available records returned to it by your Command object.

    So, here's the code that backs this up:
    VB Code:
    1. 'here's your code
    2.  If IsNumeric(txtSearchCase.Text) Then
    3.             search = "SELECT * FROM CapRec WHERE CaseNumber LIKE '%" & txtSearchCase.Text & "%';"
    4.  
    5.             'txtCaseNum.Text = drSqlDataReader1.Item(0)
    6.  
    7.         Else
    8.             search = "SELECT * FROM CapRec WHERE LastName LIKE '%" & txtSearchCase.Text & "%';"
    9.         End If
    10.  
    11. Dim SqlConnection1 As New SqlConnection(myConnectionString)
    12. Dim SqlCommand1 As New SqlCommand
    13. Dim dr As  SqlDataReader
    14. SqlCommand1.Connection = SqlConnection1
    15. SqlCommand1.CommandText = search
    16. SqlCommand1.CommandType = CommandType.Text
    17.  
    18. 'open the data connection
    19. SqlConnection1.Open
    20. 'assign the results of SqlCommand1's execute reader method to the datareader
    21. dr= SqlCommand1.ExecuteReader()
    22.  
    23. 'I always test for rows
    24. If dr.HasRows Then
    25.     'the following line will keep executing the Read method as long as there is more records to read
    26.    While dr.Read  
    27.       'FieldCount returns how many columns are in the current row
    28.       'I'm just using it here to dump your returned data to the
    29.       'console window
    30.        For i As Integer = 0 to dr.FieldCount-1
    31.         'Here I am testing for DbNull.Value, making sure there is actual
    32.         'data in the specific column index for the current record
    33.         '  and writing the values to the Visual Studio Output window
    34.          If Not dr(i) is DbNull.Value Then Debug.WriteLine(dr(i))
    35.       Next
    36.    End While
    37. End If
    38. 'close up
    39. dr.Close
    40. SqlConnection1.Close
    Last edited by nemaroller; Aug 4th, 2004 at 06:17 PM.

  3. #3
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843
    Nicely put nema,

    I'm new to this .Net stuff and while I've been able to muddle my way through data access,
    spelling it out that way helps a bit.

    Merry Christmas

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    I am far from expert at this stuff. Do you know how to return your data to a different for then the one you used to search? My "Hasrows" is not working either. Does anyone know why? Here is my coding so far:
    VB Code:
    1. Dim SqlConn As New SqlConnection("data source=localhost;initial catalog=crdatabase;integrated security=SSPI;persist security info=False;workstation id=CRSERVER;packet size=4096")
    2.         Dim cmdSqlCommand1 As SqlCommand = New SqlCommand("SELECT * FROM tblHUDdata", SqlConn)
    3.  
    4.         'Opens the DataReader to read data from the tblHUDdata table
    5.         SqlConn.Open()
    6.         Dim drSqlDataReader1 As SqlDataReader = cmdSqlCommand1.ExecuteReader()
    7.         drSqlDataReader1.Read()
    8.         Dim search As String
    9.  
    10.         If IsNumeric(txtSearchCase.Text.Substring(0)) Then
    11.             search = "SELECT * FROM CapRec WHERE CaseNumber LIKE '" & txtSearchCase.Text & "%';"
    12.  
    13.             'txtCaseNum.Text = drSqlDataReader1.Item(0)
    14.  
    15.         Else
    16.             search = "SELECT * FROM CapRec WHERE LastName LIKE '" & txtSearchCase.Text & "%';"
    17.         End If
    18.  
    19.         Dim dr As SqlDataReader
    20.         Dim index As Integer
    21.         cmdSqlCommand1.Connection = SqlConn
    22.         cmdSqlCommand1.CommandText = search
    23.         cmdSqlCommand1.CommandType = CommandType.Text
    24.  
    25.         If drSqlDataReader1.HasRows Then
    26.             While drSqlDataReader1.Read
    27.                 For index = 0 To drSqlDataReader1.FieldCount - 1
    28.                     If Not drSqlDataReader1(index) Is DBNull.Value Then Debug.WriteLine(drSqlDataReader1(index))
    29.                 Next
    30.             End While
    31.         End If
    32.         'close up
    33.         drSqlDataReader1.Close()
    34.         SqlConn.Close()

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Thanks J....

    Brenda...

    VB Code:
    1. Dim cmdSqlCommand1 As SqlCommand = New SqlCommand("SELECT * FROM tblHUDdata", SqlConn)
    2.  
    3.         'Opens the DataReader to read data from the tblHUDdata table
    4.         SqlConn.Open()
    5.         Dim drSqlDataReader1 As SqlDataReader = cmdSqlCommand1.ExecuteReader()
    6.         drSqlDataReader1.Read()
    7.         Dim search As String

    This part of your code does not do anything useful as far as I can tell. Use the code exactly as I posted earlier instead. you have declared a datareader called 'dr', but don't use it at all. Further, you are using cmdSqlCommand1 again at the bottom, but never used the ExecuteReader method on it, so you won't get any results.

    Also note; Connections can only have ONE datareader assigned at any time. And once they are read, they are exhausted, unless you execute another command, and assign its output back to the same reader.

    Again, use the code I posted earlier verbatim, with inclusion of assigning your connectionstring to myconnectionstring.
    Last edited by nemaroller; Aug 5th, 2004 at 11:39 AM.

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