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
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With