[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

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();
0
soccerman777
Asked:
soccerman777
  • 5
  • 3
1 Solution
 
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
 
Anurag ThakurCommented:
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now