How to get next dataset variable in c# [Resolved]

Posted by Karthik2010_Mca under C# on 9/17/2013 | Points: 10 | Views : 1126 | Status : [Member] | Replies : 3
Hi,

I have 5 rows in a dataset. I want get and execute the data from dataset(ds) one by one. How can I do the same.

Ex:
SqlParameter[] arParameters = new SqlParameter[1];
arParameters[0] = new SqlParameter("@iBatchID", objBatchContent.BatchID);
ds = dh.ExecuteDataSet("[dbo].[uspGetInterimBatchID]", CommandType.StoredProcedure, arParameters);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
int name=0;
for(int i=0;i<ds.Tables[0].Rows.Count; i++) // Here I have morethan 5 rows in the ds, I want get the data one by one from the ds.
{
name = Convert.ToInt32(ds.Tables[0].Rows[0]["I_Interim_batch_ID"]);
CenterManager.CopyInterimBatchContents(Convert.ToInt32(ddlBatch.SelectedValue), name.ToString(), sCreatedBy, dtCreatedOn);
}
}
}

Karthik


Responses

Posted by: Allemahesh on: 9/17/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Dear Karthik,

Here is an alternative way to do this:-

if (ds != null && ds.Tables[0].Rows.Count > 0)

{
foreach (DataTable dt in ds.Tables)
{
int name = 0;
foreach (DataRow dr in dt.Rows)
{
name = Convert.ToInt32(dr["I_Interim_batch_ID"]);
CenterManager.CopyInterimBatchContents(Convert.ToInt32(ddlBatch.SelectedValue), name.ToString(), sCreatedBy, dtCreatedOn);
}
}
}


Happy Coding

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/17/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Karthik,

I have modify the your code. You can use the below code to get your morethan 5 rows

SqlParameter[] arParameters = new SqlParameter[1]; 

arParameters[0] = new SqlParameter("@iBatchID", objBatchContent.BatchID);
ds = dh.ExecuteDataSet("[dbo].[uspGetInterimBatchID]", CommandType.StoredProcedure, arParameters);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
int name=0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
name = Convert.ToInt32(dr["I_Interim_batch_ID"]);
CenterManager.CopyInterimBatchContents(Convert.ToInt32(ddlBatch.SelectedValue), name.ToString(), sCreatedBy, dtCreatedOn);
}
}


Here I want to tell you some thing
1. As you said that "I have 5 rows in a dataset". This is wrong. There is not rows present in any data-set. I mean to say that ds.Tables[0].Rows means there are 5 rows in datatable and not dataset.
2. foreach (DataRow dr in ds.Tables[0].Rows) and for(int i=0;i<ds.Tables[0].Rows.Count; i++) are doing the same thing. So I have remove the one.

Happy Coding

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/17/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Karthik,

If you want to loop the datatable from ds then you can use the below code:-

if (ds != null && ds.Tables[0].Rows.Count > 0)

{

foreach (DataTable dt in ds.Tables)
{
int name = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
name = Convert.ToInt32(dt.Rows[0]["I_Interim_batch_ID"]);
CenterManager.CopyInterimBatchContents(Convert.ToInt32(ddlBatch.SelectedValue), name.ToString(), sCreatedBy, dtCreatedOn);
}
}
}


Happy Coding

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response