How to dump more than one row from table into the array !!!! please Help
-
hello , my self ankur , i am very new to .net (C#) I had made an function in vb .net but same logic is not workin in C# please help. I had Written an Following Code . try { da = new SqlDataAdapter(Cmd); dt = new DataTable(); da.Fill(dt); int i=0,j=0,p=0,k=0,m=0; string[] arr = new string[((dt.Columns.Count) * 3)]; if (dt.Rows.Count <= 0) return arr; for (j = 0; j <= dt.Rows.Count - 1; j++) { if ((j == 0)) { for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[i] =dt.Rows[0].ItemArray[i].ToString(); //(dt.Rows[0].Item(i)); p = i; } } if ((j == 1)) { k = p + 1; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[k] =dt.Rows[1].ItemArray[i].ToString(); k = k + 1; } } if ((j == 2)) { m = k; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[m] =dt.Rows[2].ItemArray[i].ToString(); m = m + 1; } //Next } }
-
hello , my self ankur , i am very new to .net (C#) I had made an function in vb .net but same logic is not workin in C# please help. I had Written an Following Code . try { da = new SqlDataAdapter(Cmd); dt = new DataTable(); da.Fill(dt); int i=0,j=0,p=0,k=0,m=0; string[] arr = new string[((dt.Columns.Count) * 3)]; if (dt.Rows.Count <= 0) return arr; for (j = 0; j <= dt.Rows.Count - 1; j++) { if ((j == 0)) { for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[i] =dt.Rows[0].ItemArray[i].ToString(); //(dt.Rows[0].Item(i)); p = i; } } if ((j == 1)) { k = p + 1; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[k] =dt.Rows[1].ItemArray[i].ToString(); k = k + 1; } } if ((j == 2)) { m = k; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[m] =dt.Rows[2].ItemArray[i].ToString(); m = m + 1; } //Next } }
What do you mean by "not working?"
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
-
hello , my self ankur , i am very new to .net (C#) I had made an function in vb .net but same logic is not workin in C# please help. I had Written an Following Code . try { da = new SqlDataAdapter(Cmd); dt = new DataTable(); da.Fill(dt); int i=0,j=0,p=0,k=0,m=0; string[] arr = new string[((dt.Columns.Count) * 3)]; if (dt.Rows.Count <= 0) return arr; for (j = 0; j <= dt.Rows.Count - 1; j++) { if ((j == 0)) { for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[i] =dt.Rows[0].ItemArray[i].ToString(); //(dt.Rows[0].Item(i)); p = i; } } if ((j == 1)) { k = p + 1; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[k] =dt.Rows[1].ItemArray[i].ToString(); k = k + 1; } } if ((j == 2)) { m = k; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[m] =dt.Rows[2].ItemArray[i].ToString(); m = m + 1; } //Next } }
I'm not really sure exactly what you're trying to accomplish here, but I think this code below might suit you better:
string[] arr = new string[dt.Rows.Count * dt.Columns.Count]; int i = 0; int j = 0; foreach (DataRow dr in dt.Rows) { j = 0; foreach (DataColumn dc in dt.Columns) { arr[i] = dr.ItemArray[j].ToString(); j++; i++; } }
The results of this would be an array structured as: [0] = row1 col1 [1] = row1 col2 [2] = row1 col3 [3] = row2 col1 ... Hope that helps, if it doesn't be a little more clear on what you're trying to accomplish and maybe we can come up with something better.- Arcond