• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

ColumnName property is not returning correct value.

So I'm trying to pull the column name dynamically for a user chosen table using some of the below code. For some reason when I call DataColumn.ColumnName to a messagebox it shows as expected. Yet when I try to assign it to an array for later processing I'm only able to get "CustomStatus" out.
foreach(DataColumn col in tmpTable1.Columns)
{
	colInfo[x,0] = col.ColumnName.ToString(); //Returns CustomStatus as shown through the loop below
	colInfo[x,1] = col.Unique.ToString();
	colInfo[x,2] = row[col].ToString();
	if(col.Unique == true)
	{
		y++;
	}
	//MessageBox.Show("ColumnName: "+col.ColumnName.ToString()); //Show correctly value
}
 
while(x <= colInfo.GetUpperBound(0))
{
	while(y <= colInfo.GetUpperBound(1))
	{
		MessageBox.Show("colInfo["+x.ToString()+", "+y.ToString()+"]="+colInfo[x,y].ToString()); //This is where I'm seeing ColumnName return "CustomStatus"
		y++;
	}
	x++;
}

Open in new window

0
Zexks
Asked:
Zexks
  • 2
  • 2
1 Solution
 
udaya kumar laligondlaTechnical LeadCommented:
it looks like the variable is not reset
in line no 12 reset the y value as y =0;
0
 
ZexksAuthor Commented:
My bad missed those from the copy. But yeah they're reset. I reset them before almost every loop whether I'll need them or not, then just go back later and delete the resets when they're not used in the subsequent loop. The code below is from Init to "failure"
colInfo = new string[tmpTable1.Rows.Count, 3];
				MessageBox.Show(colInfo.Length.ToString());
				foreach(DataRow row in tmpTable1.Rows)
				{
					foreach(DataColumn col in tmpTable1.Columns)
					{
						string tmp = col.ColumnName.ToString();
						colInfo[x,0] = tmp;
						MessageBox.Show("After colInfo Name Assign: " +colInfo[x,0].ToString());
						colInfo[x,1] = col.Unique.ToString();
						colInfo[x,2] = row[col].ToString();
						if(col.Unique == true)
						{
							y++;
						}
						//MessageBox.Show("AllowDBNull: "+col.AllowDBNull.ToString());
						//MessageBox.Show("AutoIncrement: "+col.AutoIncrement.ToString());
						//MessageBox.Show("AutoIncrementSeed: "+col.AutoIncrementSeed.ToString());
						//MessageBox.Show("AutoIncrementStep: "+col.AutoIncrementStep.ToString());
						//MessageBox.Show("ColumnName: "+col.ColumnName.ToString());
						//MessageBox.Show("DataType: "+col.DataType.ToString());
						//MessageBox.Show("DefaultValue: "+col.DefaultValue.ToString());
						//MessageBox.Show("MaxLength: "+col.MaxLength.ToString());
						//MessageBox.Show("Unique: "+col.Unique.ToString());
						//MessageBox.Show("row[col]: "+row[col].ToString());
					}
					x++;
				}
				
				x=0;
				y=0;
				while(x <= colInfo.GetUpperBound(0))
				{
					while(y <= colInfo.GetUpperBound(1))
					{
						MessageBox.Show("colInfo["+x.ToString()+", "+y.ToString()+"]="+colInfo[x,y].ToString());
						y++;
					}
					x++;
				}

Open in new window

0
 
udaya kumar laligondlaTechnical LeadCommented:
can you dump all the messgaes that you are displaying into a file and post here. or post the entire form code and the table definition if possible
0
 
ZexksAuthor Commented:
My Bad, I figured it out, it's overwriting previous values and not incrementing correctly therefor I'm only seeing the column name for the last column which just happens to be "CustomStatus" so it's working, I apparently just haven't implemented it correctly yet :P.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now