?
Solved

Get single value from SqlDataReader

Posted on 2012-08-15
4
Medium Priority
?
935 Views
Last Modified: 2012-08-17
Hello.

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.
0
Comment
Question by:Haugenwebdesign
4 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 38296063
if you know the index and type of the object you can just

//if this was an int32
reader.GetInt32(yourIndexNumber);
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 2000 total points
ID: 38296320
try this
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx
.....
Int32 newProdID = (Int32)cmd.ExecuteScalar();
otherFunction(newProdID);
...............

 private void otherFunction(Int32 readerValue)
    {
        Response.Write(readerValue);
    }

Open in new window

0
 
LVL 4

Expert Comment

by:Friman001
ID: 38299176
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
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38299329
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)

            myConnection.Open()
            ' 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.Close()
            myReader = myCommand.ExecuteReader
            ' Databind your DetailsWiew here
           
            myReader.Close()
            myConnection.Close()
        End Using


Alan ";0)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

829 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