I have a function which have return type DataRow and the parameters for the fucntion
are the parameters for the stored procedure.

I have to execute the Stored Procedure with given parameters and populate the datarow so that
the return will be the populated DataRow with the output of the stored procedure.

Again I have to retrieve data from the same datarow, perform some calculations in another function
which is going to use above datarow values, and give result in double or int datatype.

I have executed the Stored Procedure and now i m trying to store the data in the datarow
so that I can further retrieve it.

do anybody have idea how to insert and retreive data from DataRow in above case.

Thanks in advance,

Dani AI

Generated

A single DataRow can be returned from the stored-proc helper and later read by another method, but there are a few pitfalls and better patterns worth following.

The simplest (and least fragile) approach is either to accept a DataRow as a parameter in the calculator method, or — preferably — map the DataRow into a small strongly-typed result object (DTO) and pass that. Direct indexing (as shown by ) works, but it’s easy to hit DBNull or invalid-cast exceptions. Use null-safe accessors and checks instead of blind casts.

Example (null-safe reads with modern helpers):

// safe reads — Field<T> returns nullable types when used as Field<T?>
int? optionalCount = dr.Field<int?>("OptionalCount");
double amount = dr.IsNull("Amount") ? 0.0 : dr.Field<double>("Amount");

Better design: map to a DTO immediately after the DataRow is obtained, then discard the DataSet/DataRow. This avoids lifetime problems if the DataSet/connection is closed or disposed, and makes tests and calculations clearer:

class SpResult { public int Id { get; set; } public double Amount { get; set; } }

SpResult Map(DataRow dr) => new SpResult {
    Id = dr.Field<int>("Id"),
    Amount = dr.Field<double?>("Amount") ?? 0.0
};

Practical tips and gotchas (ties to earlier replies): the nullable suggestion from is useful — combine it with Field<T?>. ’s foreach is appropriate when enumerating rows, but for a single-row SP result it’s simpler to read the first row or map it immediately. Always check Columns.Contains before reading an optional column, handle culture when parsing strings to numbers (TryParse with InvariantCulture), and dispose connections/adapters. If performance or memory is a concern, consider a DataReader and map directly to the DTO rather than filling a DataSet.

Recommended Answers

All 8 Replies

I'm not sure if this is your question but are you just trying to read and write data to the data row? If so, then just do this:

To Read:

int myIntVar = (int)myDataRow["ColumnName1"];

string myStringVar = (string)myDataRow["ColumnName2"];

etc...

To Write:

myDataRow["ColumnName1"] = myIntVar;
myDataRow["ColumnName2"] = myStringVar;

etc...

Off the top of my head I can't remember which cases are which, but some types need to use the Convert methods instead of the straight casts.

You can also try and use nullable types (assuming you are using .NET 2.0 and up) - instead of declaring your variables like:

int myFieldValue ....

you can simply do the following:

int? myFieldValue ...

I am calling a stored procedure which returns some parameters and storing these parameters in a datarow say "drSPParameter", in a function say "InsertParameter".
Now this is one function, now I have another function where I want to use some of these parameters i.e. want to extract some parameters from datarow in another function.
i.e. retrieving data from these function's datarow parameter.Consider this function name as
"RetrieveParameter"

How can I do this?

Hi Can you please provide me source i will guide to solve it

For Retrieving records from data row use the following code

where ds is the dataset

foreach(DataRow dr in ds.Tables[0].Rows)
{
            c1  = Convert.ToString(dr["ColumeName1"]);
            c2 = Convert.ToString(dr["ColumeName2"]);
            c3 = Convert.ToString(dr["ColumeName3"]);
 }

private DataRow FetchSPParameter(string a,string b, int i)
{
drSPParameter=function to get the FirstRow from datatable of dataset("FetchSPParameter", "SPParameter", parameters);
}

where drSPParameter is the datarow where i m storing the output of the function which gets the datarow from the datatable "SPParameter" of the dataset , the second parameter
and parameters are the parameters from the fucntion to the stored procedure

"FetchSPParameter" is the stored procedure

and function returns "drSPParameter" datarow

I want to use some parameters from "drSPParameter" datarow in another function
say
private double Calculatethis()

which accepts some parameters of the datarow "drSPParameter"
My question is how can i use it in private double Calculatethis()?

Let Assume that your firstrow funcation returns a row of an employee Table

Retrive the records

int c1 = Convert.ToInt32(dr["EMPID"]);
string c2 = Convert.ToString(dr["EMPNAME"]);
double c3 = Convert.ToDouble(dr["Sal"]);

pass the variable c3 as paramter to your calculatethis function

private double Calculatethis(double c3)
{
double bonus=c3 +(c3*25/100)
return bonus
}

please letme know you finding difficult to retrieve a datarow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.