Get single value from SqlDataReader

Posted on 2012-08-15
Last Modified: 2012-08-17

Im looking for how i can get a single value out of a datareader. Normaly i would use executescalar, but i'm using a datareader to fill a DetailsWiew. I need one of the values in another function, and it would be easiest if i could get the value from the datareader and pass it to the other functon. The datareader contains only one line.
Question by:Haugenwebdesign
    LVL 22

    Expert Comment

    if you know the index and type of the object you can just

    //if this was an int32
    LVL 18

    Accepted Solution

    try this
    Int32 newProdID = (Int32)cmd.ExecuteScalar();
     private void otherFunction(Int32 readerValue)

    Open in new window

    LVL 4

    Expert Comment

    How about this (just off the top of my head from work):

    while (SQLDataReader.Read())
        if (SQLDataReader.GetValue("Some Index") == "Your Value")
             // Execute this code.

    Open in new window

    Ryan F
    LVL 26

    Expert Comment

    by:Alan Warren
    Hi Haugenwebdesign,

    Try using .ExecuteReader to populate the reader.

    The following example uses an Access2007 connection, modify for SQL as required.

    Dim SingleValue as whatever
    Using myConnection As New System.Data.OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SomeConnection").ConnectionString)

                Const SQL As String = "SELECT * FROM [tblData_PDF] WHERE [ID] = @ID"
                Dim myCommand As New System.Data.OleDb.OleDbCommand(SQL, myConnection)

                ' Dim myCommand As New SqlCommand(SQL, myConnection)
                myCommand.Parameters.AddWithValue("@ID", ID)

                ' this needs to be an SqlDb.OleDbDataReader if using an SqlDb connection
                Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand.ExecuteReader
                If myReader.Read Then
                    SingleValue = myReader("TheFieldName")
                    ' you may be able to bind your DetailsWiew here,
                    ' if not, close the reader and open it again.
                End If
                myReader = myCommand.ExecuteReader
                ' Databind your DetailsWiew here
            End Using

    Alan ";0)

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now