Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am returning one row from the database, and I want to convert the SQLDataReader to a string format, so I can pass it to my webservice.

        Dim rdr As SqlDataReader = sqlcmd.ExecuteReader

        If rdr.HasRows Then
            rdr.Read()
            GetInvHeaderValue = Convert.ToString(rdr.Read())
            Return GetInvHeaderValue
        Else
            GetInvHeaderValue = "<ERR>No Records Returned</ERR>"
        End If

How would I convert a SQLDataReader to a string?

Is there a better alternative?

share|improve this question
    
A little more information would be useful, such as what data is being returned? Is it scalar? Is all the row information needed? –  Wade73 Oct 29 '10 at 12:19
    
No sure I understand what you mean by "what" data is being returned? The row contains about 8 fields. The recordset is returning only one record. Its not scalar (first column of the first row). I need to return all the fields. HTH –  user279521 Oct 29 '10 at 12:20
add comment

1 Answer

up vote 1 down vote accepted

rdr.Read() moves the DataReader to the next records and returns if there is a next record at all. So you can write:

Dim GetInvHeaderValue As Object
While rdr.Read()
   GetInvHeaderValue  = rdr(0)'if this value is in Column-Index 0'
   GetInvHeaderValue  = rdr("GetInvHeaderValue")'if a Column with this name exists'
   GetInvHeaderValue  = rdr.GetString(0)'returns a String representation(there are getter for all common types)'
End While

You are only converting the Boolean that indicates if there is a next record to a String("True"/"False").

Have a look at MSDN for further onformations.

share|improve this answer
    
So you are saying I would have to read each field individually, as opposed to converting the whole row into string format? My end goal is to have that record passed to my webservice, which will return the results to the client in XML. –  user279521 Oct 29 '10 at 14:03
    
It wouldn't make sense to return the whole row as String. Use Linq, Dataset.ReadXML or a custom class. Maybe this link helps: forums.asp.net/p/1587381/4013756.aspx –  Tim Schmelter Oct 29 '10 at 14:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.