Arbnor Salihu

Arbnor Salihu

  • NA
  • 15
  • 690

C# get the value of certain item in listbox

Nov 9 2016 4:29 PM

I'm trying to get the value of certain item from listBox, because in my listBox1 I have two different values that have comes from dataGridView. Now, I need to separate these two items separately because it must add to the database in two different columns.

Now, how to to get only value of Name, and only value of Surname.

I tried like the following code, but I get an error(invalidargument=value of '1' is not valid for 'index'. parameter name: index)

 
 
 
DataGridViewCell cell = null;
foreach (DataGridViewCell selectedCell in dataGridView1.SelectedCells)
{
cell = selectedCell;
break;
}
if (listBox1.Items == null)
{
MessageBox.Show("Please, select an Employee!");
}
else
{
string str = "";
for (int i = 0; i <= listBox1.Items.Count-1; i++)
{
if (listBox1.Items[i] != null)
{
if (str == "")
{
str = listBox1.Items[i].ToString();
}
else
{
str += "," + listBox1.Items[i].ToString();
}
}
}
cmd.Parameters.AddWithValue("@empName", listBox1.Items[0].ToString());
cmd.Parameters.AddWithValue("@empSurname", listBox1.Items[1].ToString());

Attachment: Capture.rar

Answers (2)