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(Configuratio nSettings. AppSetting s["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.StoredProcedur e;
// 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();
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(Configuratio
connstored.Open();
// 1. create a command object identifying
// the stored procedure
SqlCommand cmd = new SqlCommand("sp_LoadTempIOD
// 2. set the command object so it knows
// to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedur
// 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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 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
ASKER
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];
}
}
{
foreach (DataColumn dc in dt.Columns)
{
object value = dr[dc];
}
}
ASKER
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?
'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.
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.
ASKER
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'
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'
ASKER
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.
If you could tell me how to get a recordcount from rdr = rdr.ExecuteReader(); I would appricaiate it.
ASKER