3

think there is a problem at
cmd.Parameters.AddWithValue("@name", listView1.SelectedItems ); anybody have idea for that??

 private void Form_Load(object sender, EventArgs e)
    {
        SqlConnection cnn = new SqlConnection(tools.ConnectionString);
        SqlCommand cmd = new SqlCommand("select * from Employees",cnn);
        cnn.Open();
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (dr.Read())
        {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = dr["FirstName"].ToString();
            lvi.SubItems.Add(dr["LastName"].ToString());
            listView1.Items.Add(lvi);
        }
        cnn.Close();
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection cnn = new SqlConnection(tools.ConnectionString);
        SqlCommand cmd = new SqlCommand("select EmployeeId,BirthDate from Employees where FirstName = @name  ",cnn);
        cmd.Parameters.AddWithValue("@name", listView1.SelectedItems );
        cnn.Open();
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (dr.Read())
        {
            MessageBox.Show("Id= "+dr["EmployeeID"].ToString() + "\nBirth Date= "+dr["BirthDate"].ToString());
        }
        cnn.Close();
    }

thanks

2
  • Wel what is the type of the selected item. And are you aware that you are trying to pass in SelectedItems which is a collection of items not just one? I think what you may want is SelectedItem singular Commented Aug 8, 2013 at 20:45
  • are you sure listView1.SelectedItems is the correct data type? Commented Aug 8, 2013 at 20:47

2 Answers 2

2

This is what you need to change.

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if(listView1.SelectedItems.Count > 0)
    {
        SqlConnection cnn = new SqlConnection(tools.ConnectionString);
        SqlCommand cmd = new SqlCommand("select EmployeeId,BirthDate from Employees where FirstName = @name  ",cnn);
        cmd.Parameters.AddWithValue("@name", listView1.SelectedItems[0].Text );
        cnn.Open();
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (dr.Read())
        {
            MessageBox.Show("Id= "+dr["EmployeeID"].ToString() + "\nBirth Date= "+dr["BirthDate"].ToString());
        }
        cnn.Close();
    }

}
Sign up to request clarification or add additional context in comments.

5 Comments

yes i want to get name from listview and than i want to get name's id and birth date from db. there 1 table on the db and query is working. but i cant use selected item on listview..
its windows form.and its working with cmd.Parameters.AddWithValue("@name", listView1.SelectedItem[0].Text ); but only 1 time for second time it gives me error like this InvalidArgument=Value of '0' is not valid for 'index'.
i debug it and im sorry i type it wrong when i write listView1.SelectedItem its not workng it said ..does not contain any definiton for selecteditem. it works ..SelectedItems[0].Text and works only 1 time and for second time it gives error which says ArgumentOutofrange exeption was handled. InvalidArgument=Value of '0' is not valid for 'index'
YEs finally it works thanks:)and putting"if(listView1.SelectedItems.Count > 0)" line to the top is great idea cuz i try to put it just before the "cmd.Parameters.AddWithValue("@name", listView1.SelectedItems[0].Text);" Anyway thansk again:)
no problem. Mark the answer as solved and we can delete all these comments to keep the QA clean
0

I would imagine selected items is a collection, try the selected item from the collection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.