Stored Procedure and DataAdapter

  • Thread starter Thread starter Wayde
  • Start date Start date
W

Wayde

Please help me if you know the answer to this. I want to
create a dataAdapter, that fills a dataSet. However, I
have a stored procedure that I want to use, that takes one
int as input. I have been trying this for a few hours now,
and have come no closer.
 
Wayde,

When you use the data adapter to fill a dataset, you can set the
SelectCommand to a SqlCommand where the CommandType property is
CommandType.StoredProcedure.

The CommandText property would be set to the name of the stored
procedure.

Then, call the CreateParameter method on the SqlCommand.

On that instance that is returned from the call to CreateParameter, set
the SqlDbType property to SqlDbtype.Int. Set the parameter value as well.

Then, call the Add method on the SqlParameterCollection returned by the
Parameter property and add the parameter.

Finally, you can call Fill to get your data set.

Hope this helps.
 
Hi, this is the code I am trying. I cannot get it to work,
I think maybe I did not understand some of what you said.
Please help if you can

SqlDataAdapter testDataAdapter = new SqlDataAdapter();
SqlCommand testCommand =
testDataAdapter.SelectCommand;
testCommand.CommandType=
CommandType.StoredProcedure;

testCommand.CommandText="depreciation";
testCommand.Parameters.Add
("@CategoryID",SqlDbType.Int);
SqlParameter testInput =
testCommand.Parameters.Add("@CategoryID", SqlDbType.Int);
testInput.Direction =
ParameterDirection.Input;
testInput.Value= Convert.ToInt32
(index);

DataSet testDataSet = new DataSet
();
testDataAdapter.Fill
(testDataSet, "Table1");
//dataGrid1.DataBindings =
testDataSet.Tables["Table1"];
dataGrid1.DataSource = testDataSet;
dataGrid1.DataMember = "invName";
-----Original Message-----
Wayde,

When you use the data adapter to fill a dataset, you can set the
SelectCommand to a SqlCommand where the CommandType property is
CommandType.StoredProcedure.

The CommandText property would be set to the name of the stored
procedure.

Then, call the CreateParameter method on the SqlCommand.

On that instance that is returned from the call to CreateParameter, set
the SqlDbType property to SqlDbtype.Int. Set the parameter value as well.

Then, call the Add method on the
SqlParameterCollection returned by the
Parameter property and add the parameter.

Finally, you can call Fill to get your data set.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



Wayde said:
Please help me if you know the answer to this. I want to
create a dataAdapter, that fills a dataSet. However, I
have a stored procedure that I want to use, that takes one
int as input. I have been trying this for a few hours now,
and have come no closer.


.
 
Back
Top