Newbie alert! Looping through a Dataset

R

RSH

I am struggling a bit with trying to access data values from a dataset.
What am I doing wrong here???

Thanks,
RSH


private void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)

{

if(e.RowIndex >= 0)

{

SqlConnection oCon = new SqlConnection("server='ENGINE'; Integrated
Security='SSPI'; database='20039999'");

DataSet ds = new DataSet();

oCon.Open();

SqlDataAdapter oCmd = new SqlDataAdapter("select ID,FirstName,LastName from
Employee WHERE ID = " + dataGridView1.Rows[e.RowIndex].Cells[1].Value ,
oCon);

oCmd.Fill(ds, "Employee");

for (int i = 0; i <= ds.Tables["Employee"].Rows.Count; i++)

{

for (int j = 0; j <= ds.Tables["Employee"].Columns.Count; j++)

{

label1.Text = label1.Text + " " + ds.Tables["Employee"].Rows[j].ToString
+ "\n"; <-------- ERRORS HERE

}

}

oCon.Close();

}

}
 
M

Marina

First off, please never post a question with the symptom being 'there is an
error'. You need to tell us exactly what the error is and the message.

Just from looking at your code, your loops are wrong. You are going to go
past the end of the rows and columns collections, as you are going from 0 to
Count instead of to Count-1.
 
N

Nicholas Paldino [.NET/C# MVP]

RSH,

Well, what is happening? Are you getting an exception?
 
R

RSH

Sorry about that.

Okay I fixed most everything except creating a string dynamically.

I am trying to build the string dynamically:

string strDsp;

for (int i = 0; i < ds.Tables["Employee"].Rows.Count; i++)

{

for (int j = 0; j < ds.Tables["Employee"].Columns.Count; j++)

{

strDsp = strDsp + ds.Tables["Employee"].Rows.ItemArray[j].ToString();

}

}

I am getting the error:
Error 1 Use of unassigned local variable 'strDsp' Form1.cs 68 25
InitialData
 
J

jmcgrew

You're getting that error because strDsp has no value at first, so the
first time you hit that line inside the innermost loop, you're
appending to an uninitialized variable. You could fix it by changing
the declaration of strDsp to:

string strDsp = "";

However, with a loop like that, it's best to use StringBuilder to
create the string instead of the + operator. Something like this:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < ds.Tables["Employee"].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables["Employee"].Columns.Count; j++)
{
sb.Append(ds.Tables["Employee"].Rows.ItemArray[j].ToString());
}
}
string strDsp = sb.ToString();
// do whatever you were going to do with strDsp

Jesse
 
J

James Curran

Well, while we are on the subject, this is a neater and probably more
effiecent way of handling the task:

DataSet ds = ....
// :
oCmd.Fill(ds, "Employee");
// :
StringBuilder sb = new StringBuilder();
foreach(DataRow dr in ds.Tables["Employees"].Rows)
{
foreach(DataColumn col in ds.Tables["Employees"].Columns)
{
sb.Append(dr[col].ToString());
}
}
string strDsp = sb.ToString();

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top