C# .NET - is there nobody who can tell me? - Asked By Priyanka on 25-Sep-10 11:20 PM

 can anybody tell me how to write query to delete selected row in list view from database...
 i am writing query like this but it is not working... "Delete listview1.SelectedItems(0) from Table1";
plz suggest me right query i will be very thankful of urs....


is there nobody who can tell me?

Re : Delete Listview Item - Super Man replied to Priyanka on 25-Sep-10 11:51 PM

Are you displaying any unique field in the listview means Primay key field.

foreach(System.Windows.Forms.ListViewItem eachItem in this.listView1.SelectedItems)
{


//get the unique field value and write the code to delete from database based on the PK value.

listView1.Items.Remove(eachItem);
}
Kirtan Patel replied to Priyanka on 26-Sep-10 12:07 AM
 
public void LoadDataInListView()
    {
       /* loading Data in ListView From database */
      SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
      con.Open();
      SqlCommand comm = new SqlCommand("select * from Test", con);
      SqlDataReader reader = comm.ExecuteReader();
      listView1.Items.Clear();
      while (reader.Read())
      {
        string[] data = new string[2];
        data[0] = reader["Username"].ToString();
        data[1] = reader["Password"].ToString();
        ListViewItem liv = new ListViewItem(data);
        listView1.Items.Add(liv);
      }

    }

    private void btnDeleteSelected_Click(object sender, EventArgs e)
    {
      if (listView1.SelectedItems.Count > 0)
      {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
        con.Open();
        SqlCommand comm = new SqlCommand("delete from Test where Username=@username", con);
        comm.Parameters.AddWithValue("@username", listView1.SelectedItems[0].SubItems[0].Text);
        comm.ExecuteNonQuery(); 

        /* reaload Listview */
        LoadDataInListView();
      }
      
    }
Priyanka replied to Kirtan Patel on 26-Sep-10 12:22 AM
Thanks dude it is working ..
Goniey N (Mr. G) replied to Priyanka on 26-Sep-10 11:09 PM
-- Use Below Code :



   -- Used To Bind Data From Database To LitView...


private void Bind()

{

        SqlConnection conn = new SqlConnection("<<Your Connection String>>");

        conn.Open();

        SqlDataReader dr;

        SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee", conn);

        dr = cmd.ExecuteReader();

        while (dr.Read())

        {

          listView1.Items.Add(dr.GetValue(0).ToString());

        }

        conn.Close();

}

   -- Call Bind Function On Page Load Event...


private void Form1_Load(object sender, EventArgs e)

{

        listView1.Items.Clear();

        Bind();

}

   -- Delete The Selected Items From The Listview As Well As From The Database...


private void button4_Click(object sender, EventArgs e)

{

        foreach (ListViewItem LVI in listView1.SelectedItems)

        {

          try

          {

            SqlConnection conn = new SqlConnection("<<Your Connection String>>");

            conn.Open();

            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "DELETE FROM Employee Where EmpName='" + LVI.Text + "';";

            cmd.ExecuteNonQuery();

            conn.Close();

          }

          catch (Exception ex)

          {

            MessageBox.Show(ex.ToString());

          }

        }

        listView1.Items.Clear();

        Bind();

}

-- See Below Images For More Clear View :

   -- Select Two Items From The Listview...


   -- Delete The Selected Items From The ListView As Well As Database Also...

--It Will Work 100%...

-- Hope This Will Help You...