How to reference stored procedure in C#

I have the following stored procedure call.   How do I reference the result in the form load event and in the page output.

 SqlConnection connstored = null;
            SqlDataReader rdr  = null;

            // typically obtained from user
            // input, but we take a short cut
            int FBID = 5543917;
   
   
                  // create and open a connection object
                  connstored = new SqlConnection(ConfigurationSettings.AppSettings["myconn"]);
           
                  connstored.Open();
                  // 1.  create a command object identifying
                  //     the stored procedure
                  SqlCommand cmd  = new SqlCommand("sp_LoadTempIOD", connstored);

                  // 2. set the command object so it knows
                  //    to execute a stored procedure
                  cmd.CommandType = CommandType.StoredProcedure;

                  // 3. add parameter to command, which
                  //    will be passed to the stored procedure
                  cmd.Parameters.Add(
                        new SqlParameter("@FBID", FBID));
            cmd.Parameters.Add(
                new SqlParameter("@BOL", ""));
                  // execute the command
                  rdr = cmd.ExecuteReader();
soccerman777Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

soccerman777Author Commented:
I need to loop through this result set and do a update for each record. Please give a example.
0
Bob LearnedCommented:
I would take the result from the SqlDataReader, and put that into a DataTable, which you can reference more easily than the forward-only SqlDataReader.  Loading a DataTable is a simple as this:

DataTable dt = new DataTable("MyTable");

dt.Load(rdr).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anurag ThakurTechnical ManagerCommented:
i dont quite understand the problem
i will try to repharase it
you want to get the result set bck from the database by executing the stored procedure and then loop it around and then do some updates - am i correct

if i am on the correct path then instead of executing the executereader i will prefer to fill in my datatable or data set as i will not keep a connection till the time i loop around my result and will be updating the database

0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

soccerman777Author Commented:
I have now loaded the date into a table. Now how do I loop and reference the table?     I should have mentioned this but this is my first c#   application.
0
Bob LearnedCommented:
foreach (DataRow dr in dt.Rows)
{
    foreach (DataColumn dc in dt.Columns)
    {
          object value = dr[dc];
     }
}
0
soccerman777Author Commented:
I am getting this error loading the table

'System.Data.DataTable' does not contain a definition for 'Load'

This is the code I am using

rdr = cmd.ExecuteReader();
            DataTable dt = new DataTable("ProdResult");
            dt.Load(rdr);  


Can I name the datatable(I curently created the name prodresult) anything I want?
0
Bob LearnedCommented:
Oops, coding in the comment block (d'oh):

rdr.Load(dt);

The constructor form that I showed you is for passing in any table name that you want into the DataTable.  It is a good practice, especially if you want to serialize a DataSet/DataTable.
0
soccerman777Author Commented:
I switched my code to

rdr = cmd.ExecuteReader();
           
            DataTable dt = new DataTable("ProdResult");
            rdr.Load(dt);

I am still getting the error

'System.Data.DataTable' does not contain a definition for 'Load'
0
soccerman777Author Commented:
I went a better route. I just looped through a select statment using CURSOR in my stored procedure.  

If you could tell me how to get a recordcount  from rdr = rdr.ExecuteReader(); I would appricaiate it.
 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.