i want to populate an array element with 2 datarows at a time.
i am using javascript pausescroller and on every single array element i want to show 2 Rows in the scroller.
sample code is
Datatable tblNews;
for(int i = 0; i < tblNew.Rows.Count; i++)
{
array[i] = tblNews.Rows[i][""].ToString() + "" + tblNews.Rows[i + 1][""].ToString();
}
but problem is i am getting error that no row found at position 1;
Any solution guys
Yah, quite simply, when you're getting to the end of your tblNew.Rows.Count you're adding another one and that doesn't exist in your rows...
so a quick and dirty fix...
for(int i = 0; i < tblNew.Rows.Count-1; i++)
{
// do a quick check to make sure there is a row there to get data from
string addMe = i + 1 <= tblNews.Rows.Count-1 ? tblNews.Rows[i+1][""].ToString() : "";
array[i] = tblNews.Rows[i][""].ToString() + "" + addMe;
}
Related
How can I compare elements in one array?
if the next number in the array is greater than the previous one, then transfer these two values further ... And accordingly, if this is not so, transfer them to another method ...
double[] ipp = { 0.255, 0.232, 0.618, 0.713 };
I'm trying to do so, but the array goes beyond the number of indices
for (int i = 0; i < ipp.Length; i++)
{
if (ipp[i+1] > ipp[i]) // problem
{
// ----> send this
//(ipp[i+1];ipp[i]) values
//to another method
}
}
The problem is in the upper bound check in your for loop. When i = ipp.Length - 1 and you access array item ipp[i + 1] you access an element outside of the array bounds, therefore the problem occurs.
You need to change upper bound check to the i < ipp.Length - 1:
for (int i = 0; i < ipp.Length - 1; i++)
{
if (ipp[i + 1] > ipp[i])
{
...
}
}
My code throws this:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index error
When it finds an element in a list that have a count less that a specific number. any ideas on how to correct the code?
I've added debug.log code everywhere to determine exactly where the error happens, because no error is underlined by Visual Studio.
List<int> emptyRows = new List<int>();
for (int j = 0; j < gridPositions.Count; j++) // try to find if a row is still empty
{
Debug.Log("gridPositions[" + j + "].Count is " + gridPositions[j].Count);
Debug.Log("columns are" + columns);
if (gridPositions[j].Count == columns)
{
Debug.Log("trying to add to emptyrows");
emptyRows.Add(j);
Debug.Log("added to emptyrows and its count is " + emptyRows.Count);
}
else
{
Debug.Log("found an occupied row at row " + j);
//ERROR STRIKES HERE
}
Debug.Log("emptyRows is " + emptyRows[j]);
Debug.Log("emptyRows count is " + emptyRows.Count);
}
I expect the emptyRows to track and record all unoccupied rows, but when it fills an occupied row, it not proceeds whit the for loop and stops.
You are only adding to emptyRows if (gridPositions[j].Count == columns)
But you are accessing the emptyRows[j] on every value of j
So the emptyRows ends up having less items then the value of j is
I am planning to get an array of the averages of each column.
But my app crashes at sum[j] += int.Parse(csvArray[i,j]); due to a FormatException. I have tried using Convert.ToDouble and Double.Parse but it still throws that exception.
The increments in the for loop start at 1 because Row 0 and Column 0 of the CSV array are strings (names and timestamps). For the divisor or total count of the fields that have values per column, I only count the fields that are not BLANK, hence the IF statement. I think I need help at handling the exception.
Below is the my existing for the method of getting the averages.
public void getColumnAverages(string filePath)
{
int col = colCount(filePath);
int row = rowCount(filePath);
string[,] csvArray = csvToArray(filePath);
int[] count = new int[col];
int[] sum = new int[col];
double[] average = new double[col];
for (int i = 1; i < row; i++)
{
for (int j = 1; j < col; j++)
{
if (csvArray[i,j] != " ")
{
sum[j] += int.Parse(csvArray[i,j]);
count[j]++;
}
}
}
for (int i = 0; i < average.Length; i++)
{
average[i] = (sum[i] + 0.0) / count[i];
}
foreach(double d in average)
{
System.Diagnostics.Debug.Write(d);
}
}
}
I have uploaded the CSV file that I use when I test the prototype. It has BLANK values on some columns. Was my existing IF statement unable to handle that case?
There are also entries like this 1.324556-e09due to the number of decimals I think. I guess I have to trim it in the csvToArray(filePath) method or are there other efficient ways? Thanks a million!
So there are a few problems with your code. The main reason for your format exception is that after looking at your CSV file your numbers are surrounded by quotes. Now I can't see from your code exactly how you convert your CSV file to an array but I'm guessing that you don't clear these out - I didn't when I first ran with your CSV and experienced the exact same error.
I then ran into an error because some of the values in your CSV are decimal, so the datatype int can't be used. I'm assuming that you still want the averages of these columns so in my slightly revised verion of your method I change the arrays used to be of type double.
AS #musefan suggested, I have also changed the check for empty places to use the IsNullOrWhiteSpace method.
Finally when you output your results you receive a NaN for the first value in the averages column, this is because when you don't take into account that you never populate the first position of your arrays so as not to process the string values. I'm unsure how you'd best like to correct this behaviour as I'm not sure of the intended purpose - this might be okay - so I've not made any changes to this for the moment, pop a mention in the comments if you want help on how to sort this!
So here is the updated method:
public static void getColumnAverages(string filePath)
{
// Differs from the current implementation, reads a file in as text and
// splits by a defined delim into an array
var filePaths = #"C:\test.csv";
var csvArray = File.ReadLines(filePaths).Select(x => x.Split(',')).ToArray();
// Differs from the current implementation
var col = csvArray[0].Length;
var row = csvArray.Length;
// Update variables to use doubles
double[] count = new double[col];
double[] sum = new double[col];
double[] average = new double[col];
Console.WriteLine("Started");
for (int i = 1; i < row; i++)
{
for (int j = 1; j < col; j++)
{
// Remove the quotes from your array
var current = csvArray[i][j].Replace("\"", "");
// Added the Method IsNullOrWhiteSpace
if (!string.IsNullOrWhiteSpace(current))
{
// Parse as double not int to account for dec. values
sum[j] += double.Parse(current);
count[j]++;
}
}
}
for (int i = 0; i < average.Length; i++)
{
average[i] = (sum[i] + 0.0) / count[i];
}
foreach (double d in average)
{
System.Diagnostics.Debug.Write(d + "\n");
}
}
I have looked through several articles but non seem to answer the problem that I am facing.
I guess the easiest way to explain is to break down my current code.
for (int i = 0; i < nodelist.Count; i++)
{
XmlNode node = nodelist[i];
XmlAttributeCollection attribute = node.Attributes;
count = 0;
cmdA.CommandText = "";
cmdsqlvalues = "";
cmdtgvalues = "";
for (int n = 0; n < attribute.Count; n++)
{
string tgfield = namefieldprojarray[i, n];
int index = -1;
index = Array.IndexOf(tgfieldprojarray, tgfield);
if (index != -1)
{
cmdsqlvalues = cmdsqlvalues +""+ sqlfieldprojarray[index]+",";
cmdtgvalues = cmdtgvalues + "" + valuefieldprojarray[i, n];
}
}
if (!valuefieldprojarray[i,0].Contains("$"))
{
cmdA.CommandText = "INSERT INTO proj_tasks ('" +cmdsqlvalues +"') VALUES ('" + cmdtgvalues + "')";
}
I have an XML document with several nodes that contain many attributes that I need to populate into a SQL database.
Some nodes have more or less attributes than others
Which is why I have included an IndexOf Array and added these values to a string array.
I am trying to use the sting array "cmdsqlvalues" as the table names, but when I try and insert into the SQL table the string array is appearing as one table column and trying to insert a huge column name.
It is important that I use string array because the number of nodes and the number of attributes for each node is large and is different for each node.
If anyone has come across a similar problem and has any input or advice then that would be highly appreciated.
my For statement is not highlighting/selecting items from the listbox am missing something?
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
for (int b = 0; b < listCell.Items.Count; b++)
{
MakeReq(txtWebUpdate.Text + listId.Items[a].ToString() +
"&brand=1", listCell.Items[b].ToString());
}
}
}
this is an old vb6 code i dug up that i use to use... what it did was loop through a listbox select one entry at a time and also highlighted/selected visually the entry in the listbox. I want to do something just like that with the C# code above
Do Until lstNames.ListCount = 0
lstNames.ListIndex = 0
GetMoreNames = ""
For b = 1 To 1
GetMoreNames = GetMoreNames & lstNames.Text & ","
lstNames.RemoveItem lstNames.ListIndex
lstNames.ListIndex = lstNames.ListIndex + 1
Next b
I'm assuming the makeReq is redirecting with a querystring value? Need more code to know see what you're trying to accomplish here.
I don't know why you wouldn't just use a simple ListBox1.SelectedItem.Value;