Link to home
Start Free TrialLog in
Avatar of soccerman777
soccerman777

asked on

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();
Avatar of soccerman777
soccerman777

ASKER

I need to loop through this result set and do a update for each record. Please give a example.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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.
foreach (DataRow dr in dt.Rows)
{
    foreach (DataColumn dc in dt.Columns)
    {
          object value = dr[dc];
     }
}
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?
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.
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'
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.