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?
 
Bob LearnedConnect With a Mentor Commented:
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
 
soccerman777Author Commented:
I need to loop through this result set and do a update for each record. Please give a example.
0
 
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.