Answered by:
Passing Dataset values as variables between Stored Procedures

Question
-
User-666661156 posted
Hello,
I'm trying to pass one dataset's result as a variable in another stored procedure and this is what I have so far:
DataSet dsID = StoredProcedures.GetID((int)Session["TypeID"]); int IDValue = Convert.ToInt32(dsID.Tables[0].Rows[0]["ID"]); DataSet dsRequest = StoredProcedures.GetRequest(IDValue);
But I get the error: cannot implicitly convert to system.dataset.
I'm sure I'm going about this wrong, any ideas would be helpful. Thanks
Wednesday, August 6, 2014 4:43 PM
Answers
-
User-666661156 posted
Adding static to the dataset seemed to resolve it, so Public static Dataset GetRequest(int ID)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 6, 2014 6:01 PM
All replies
-
User103196646 posted
Hello RankOne! Thanks for your post!
Check first that database value is not null:
const int IDValue = 0; if (dsID.Tables[0].Rows[0]["ID"] != null) IDValue = Convert.ToInt32(dsID.Tables[0].Rows[0]["ID"]);
Also, are you sure that both these methods return a DataSet? It sounds like one of your calls is not returning a data set.
- StoredProcedures.GetID
- StoredProcedures.GetRequest
Regards!
Wednesday, August 6, 2014 5:06 PM -
User2103319870 posted
DataSet dsID = StoredProcedures.GetID((int)Session["TypeID"]);
DataSet dsRequest = StoredProcedures.GetRequest(IDValue);
Check if the return type of both of the above methods are DataSet or not. Most Likely your return type of method is wrong .
Sample Code
public DataSet GetID(int typeID) { DataSet ds = new DataSet(); return ds; }
Wednesday, August 6, 2014 5:08 PM -
User-666661156 posted
Based on the response, I changed it to this:
public DataSet GetRequest(int ID) { Database db = DatabaseFactory.CreateDatabase(); string sqlCommand = "dbo.GetRequest"; System.Data.Common.DbCommand cmd = db.GetStoredProcCommand(sqlCommand); cmd.CommandTimeout = 200; db.AddInParameter(cmd, "@ID", DbType.Int32, ID); DataSet ds = db.ExecuteDataSet(cmd); return ds; db.ExecuteNonQuery(cmd); }
Now the error that I get is
An object reference is required for the non-static field, method, or property 'SolutionWeb.BLL.Storedprocedures.GetRequest(int)
Wednesday, August 6, 2014 5:18 PM -
User465171450 posted
How are you accessing it? If you are doing something like:
DataSet ds = MyNamespace.MyClass.GetRequest(5);
Then this is why you have an error. You need to instantiate your class first as an object, then call the GetRequest.
So you would do:
MyNamespace.MyClass obj = new MyNamespace.MyClass(); DataSet ds = obj.GetRequest(5);
The error is telling you that you're calling it as if you didn't need to insantiate the object first, which you can't do since the method isn't a static one. Not sure if it can be static or not, it depends on the full implementation. Also, you haven't initialized a connection or ensured that it's opened and closed right before and after you call ExecuteDataSet(); Also lose the db.ExecuteNonQuery(cmd); since that will never be called.
Wednesday, August 6, 2014 5:53 PM -
User-666661156 posted
Adding static to the dataset seemed to resolve it, so Public static Dataset GetRequest(int ID)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 6, 2014 6:01 PM