how can i fetch text box value in string array and print - c#

this is my code 1 index print second index showing error "Index was outside the bounds of the array." please help what should I do?
string[] SName = Request.Form.GetValues("title");
string[] Email = Request.Form.GetValues("fname");
for (int i = 0; i <= SName.Length - 1; i++)
{
Response.Write(SName[i]);
Response.Write(Email[i]);
}

It is not necessary you get same length for both SName and Email string arrays.
Index is out of bound because length are not same.
Better way is to do it separately:
string[] SName = Request.Form.GetValues("title");
string[] Email = Request.Form.GetValues("fname");
for (int i = 0; i < SName.Length; i++)
Response.Write(SName[i]);
for (int i = 0; i < Email.Length; i++)
Response.Write(Email[i]);
If you want to print one name and email then use this:
string[] SName = Request.Form.GetValues("title");
string[] Email = Request.Form.GetValues("fname");
int iLength = -1;
if(SName.Length > Email.Length)
iLength = SName.Length;
else
iLength = Email.Length;
for (int i = 0; i < iLength; i++)
{
if(i < SName.Length)
Response.Write(SName[i]);
if(i < Email.Length)
Response.Write(Email[i]);
}
NOTE:
If you are not dealing with array of HTML element with same name then you don't have to use Request.Form.GetValues("title"). See following example:
string SName = Request.Form["title"];
string Email = Request.Form["fname"];
Response.Write(SName + " " + Email);

Your code should be.
if (SName != null)
for (int i = 0; i < SName.Length; i++)
Response.Write(SName[i]);
if (Email != null)
for (int i = 0; i < Email.Length; i++)
Response.Write(Email[i]);
The problem is that length of SName and Email are different.

You can use the below code, which gives the result in a single loop
if (SName != null && SName.Length > 0 && Email != null && Email.Length > 0)
{
for (int i = 0,j=0; i < SName.Length && j<Email.Length; i++,j++)
{
Response.Write(SName[i]);
Response.Write(Email[j]);
}
}

Related

Check duplicate and sum values in datagridview c#

I'm coding like below but it works incorrect.It perform(plus and delete) only 2->3 rows if data has 5->6 duplicate data.
Update and It works
for (int i = 0; i < dataGridView1.RowCount - 1; i++) //compare data
{
var Row = dataGridView1.Rows[i];
string abc = Row.Cells[1].Value.ToString() + Row.Cells[2].Value.ToString().ToUpper();
// MessageBox.Show(abc);
for (int j = i + 1; j < dataGridView1.RowCount; j++)
{
var Row2 = dataGridView1.Rows[j];
string def = Row2.Cells[1].Value.ToString() + Row2.Cells[2].Value.ToString().ToUpper();
if (abc == def)
{
Row.Cells[5].Value = Convert.ToDouble(Row.Cells[5].Value.ToString()) + Convert.ToDouble(Row2.Cells[5].Value.ToString());
dataGridView1.Rows.Remove(Row2);
j--;
}
}
}
This should do the trick for you:
for (int i = 0; i < dataGridView1.RowCount - 1; i++) //compare data
{
var Row = dataGridView1.Rows[i];
string abc = Row.Cells[0].Value.ToString() + Row.Cells[1].Value.ToString().ToUpper();
for (int j = i+1; j < dataGridView1.RowCount; j++)
{
var Row2 = dataGridView1.Rows[j];
string def = Row2.Cells[0].Value.ToString() + Row2.Cells[1].Value.ToString().ToUpper();
if (abc == def)
{
Row.Cells[2].Value = (int)Row.Cells[2].Value + (int)Row2.Cells[2].Value;
dataGridView1.Rows.Remove(Row2);
j--;
}
}
}
You basically need to keep track of j variable as you remove rows from the collection.
If you're a fan of LINQ and don't mind a bit of a convoluted code, here is another approach:
for (int i = 0; i < dataGridView1.RowCount; i++) //compare data
{
var R = dataGridView1.Rows[i];
var V = R.Cells[0].Value.ToString() + R.Cells[1].Value.ToString().ToUpper();
var DupRows = dataGridView1.Rows.Cast<DataGridViewRow>().Skip(i + 1).
Where(r => r.Cells[0].Value.ToString() + r.Cells[1].Value.ToString().ToUpper() == V);
R.Cells[2].Value = (int)R.Cells[2].Value + DupRows.Sum(r => (int)r.Cells[2].Value);
foreach (var DupRow in DupRows)
DupRow.Tag = "Del";
}
for (int i = 0; i < dataGridView1.RowCount; i++)
{
var R = dataGridView1.Rows[i];
if (R.Tag?.ToString() == "Del")
{
dataGridView1.Rows.Remove(R);
i--;
}
}
As a word of advice, this kind of stuff is handled far more easily in the back-end. Whatever your DataGridView is bound to, be it a DataTable or a generic collection, you should implement duplicate removal there instead of directly playing with DataGridView cells.

Set two values in one matrix

Is it possible to set two values in one for loop? I would like to create a string matrix [,], the first element (i) is variable, the second (j) is constant
int rowRun = 1;
string[,] costume = new string [elementsOfRunning, rowRun];
int columnRun = costume.GetLength(0);
for (int i = 0; i < columnRun; i++)
{
int rowOfRunning;
do
{
Console.WriteLine("Row of running (0-42)");
rowOfRunning = int.Parse(Console.ReadLine());
}
while (!(0 <= rowOfRunning && rowOfRunning <= 42));
string rowOfRunning2 = rowOfRunning.ToString();
}
And here I would like to set the i value for example: costume[i,j] = rowOfRunning; But I can't in this way.
for (int j = 0; j < rowRun; j++)
{
string comment = "";
do
{
Console.WriteLine("Write a comment: („verseny”, „terep”, „laza”, „fartlek”, „résztáv”");
comment = Console.ReadLine();
}
while (!comment.Contains(",") && comment != "verseny" && comment != "terep" && comment != "laza" && comment != "fartlek" && comment != "résztáv");
costume[i, j] = comment;
Console.WriteLine(costume[i,j]);
}
I'm not sure what you want to do but you can declare more than one variable in for loop like this:
var array = new Array[10, 10];
for (int i = 0, j = 1; i < 3; j++, i++)
{
Console.WriteLine("i:" + i + " j:" + j);
array[i,j] = Console.ReadLine();
}

Getting System.ArgumentOutOfRangeException while copy between two Datagridviews

If somebody could please help solve the following issue:
System.ArgumentOutOfRangeException : Index was out of range. Must be
non-negative and less than the size of the collection. Paarameter name
: index
The code:
for (int i = 0; i <= dataGridView2.Rows.Count ; i++)
{
for (int j = 0; j <= dataGridView3.Rows.Count; j++)
{
if (!string.IsNullOrEmpty(dataGridView2.Rows[i].Cells["supplier_name"].Value as string) && !string.IsNullOrEmpty(dataGridView3.Rows[j].Cells["brands_supplier"].Value as string))
{
if (dataGridView2.Rows[i].Cells["supplier_name"].Value.ToString() == dataGridView3.Rows[j].Cells["brands_supplier"].Value.ToString())
{
dataGridView2.Rows[i].Cells["brands_name"].Value += dataGridView3.Rows[j].Cells["brands_nume"].Value + " ";
}
}
else
{
break;
}
}
}
You try to access an element of your datagrid which doesn't exist.
You have to set your for condition to
for (int i = 0; i < dataGridView2.Rows.Count ; i++)
and
for (int j = 0; j < dataGridView3.Rows.Count; j++)
don't use <= because the index of dataGridView.Rows[] is 0 based.
For example, if your datagrid contains 3 elements you can reach them with:
var row1 = dataGrid.Rows[0]
var row2 = dataGrid.Rows[1]
var row3 = dataGrid.Rows[2]
And you try to access
var row4 = dataGrid.Rows[3] // Error because this item doesn't exist (System.ArgumentOutOfRangeException)
also, but this item doesn't exist
I think problem in cycle
for (int i = 0; i <= dataGridView2.Rows.Count ; i++)
You run one more time
use
for (int i = 0; i < dataGridView2.Rows.Count ; i++)

Store String Character in a Jagged Array

I have a string and want to store every word characters in this string in a jagged array without using a split() method, just loops. I tried this code but it didnt work,i want the result will be something like that:
sepwords[0][1] = {H,e,l,l,o};
sepwords[0][2] = {h,o,w};
sepwords[0][3] = {a,r,e};
sepwords[0][4] = {y,o,u};
The code i tried:
for (int i = 0; i < length; i++)
{
letters[i] = text[i];
}
foreach (char item in letters)
{
for (int i = 0; i < length; i++)
{
if (letters[i] != ',' || letters[i] != ';' || letters[i] != '!' || letters[i] != '?' || letters[i] != '.' || letters[i] != ' ')
{
for (int j = 0; j < length; j++)
{
sepwords[0] = new char[length];
sepwords[0][j] = letters[i];
}
}
else
{
continue;
}
}
}
sepwords[0] = new char[length];
You're overwriting every array with a new one when you reach a new word.
You can simply do this..
char[][] sepwords = new char[4][];
sepwords[0] = "Hello".ToCharArray();
sepwords[1] = "how".ToCharArray();
sepwords[2] = "are".ToCharArray();
sepwords[3] = "you".ToCharArray();

How to split get utf string

if (cmd == "card_request") {
Debug.Log("FOund cards");
ISFSObject responseParams = (SFSObject)evt.Params["params"];
Debug.Log(responseParams.GetClass("cards").ToString());
SFSArray data = (SFSArray)responseParams.GetSFSArray("cards");
Debug.Log(data.GetUtfString(0));
//for (int i = 0; i < data.GetUtfString(0).IndexOf("value"); i++) {
firstSplit = data.GetUtfString(0).Split(';');
Debug.Log(firstSplit);
//}
for (int i = 0; i < firstSplit[0].IndexOf("value"); i++) {
secondSplit = firstSplit[0].Split(':');
Debug.Log(secondSplit);
}
for (int i = 0; i < secondSplit[0].IndexOf("value"); i++) {
thirdSplit = secondSplit[0].Split(',');
Debug.Log(thirdSplit);
}
}
the data is coming fine at this line Debug.Log(data.GetUtfString(0)); But when I try to split it it gives errors. Somebody can please suggest me the effective way to split UTF string. Null exceptions occurs at secondSplit and thirdSplit

Categories