Click here to Skip to main content
16,022,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Iselibrary
{
public partial class Delete : Form
{
SqlConnection vid = new SqlConnection("Data Source=.;Initial Catalog=iselibrary;Integrated Security=True");
//private object item;

public Delete()
{
InitializeComponent();
}
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{

}

private void button1_Click_1(object sender, EventArgs e)
{
this.Close();
Home home = new Home();
home.Show();
}

private void Update_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'iselibraryDataSet1.T_BOOK_ISSUE_TABLE' table. You can move, or remove it, as needed.
this.t_BOOK_ISSUE_TABLETableAdapter.Fill(this.iselibraryDataSet1.T_BOOK_ISSUE_TABLE);

}

private void button2_Click(object sender, EventArgs e)
{
String str = "Select * from T_BOOK_ISSUE_TABLE where (USN like '%' + @search + '%')";
SqlCommand xp = new SqlCommand(str, vid);
xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = textBox1.Text;


vid.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet dat = new DataSet();
da.Fill(dat, "USN");
dataGridView1.DataSource = dat;
dataGridView1.DataSource = dat.Tables[0];
vid.Close();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}
// Delete Button Details//
private void button3_Click(object sender, EventArgs e)
{
iselibraryEntities ise = new iselibraryEntities();
foreach (var item in dataGridView1.Rows)
{
DataGridViewRow row = item as DataGridViewRow;
if (row.Selected)
{
string usn = row.Cells["USN"].Value.ToString();
var issu = ise.T_BOOK_ISSUE_TABLE.FirstOrDefault(a => a.USN.Equals(usn));
if (issu != null)
{
ise.T_BOOK_ISSUE_TABLE.Remove  /* as error occurs here I have not completed the code*/
}
}
}
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
}
}


THANK YOU

As soon as i complete ise.T_BOOK_ISSUE_TABLE. in If loop, I dont get a dropdown for Remove. Why so? and error says as:'Object Query<T_BOOK_ISSUE_TABLE>' does not contain a definition for 'remove' and no extension method 'remove' accepting a first argument of type'Object Query<T_BOOK_ISSUE_TABLE>'could be found(are you missing a directive or an assembly reference?)
Please let me know where im going wrong or anyone out there please help me to write delete button code using checkbox checked.(#windows form app) Hope anyone in forum helps....
Thank you
Posted
Updated 22-Oct-15 0:41am
v2

Hi,
instead of first or default why cant u go with find or single or where
if(issu !=null)
{
entity.tablename.remove(issu);
entity.savechanges();
}

i hope this works for u.....
 
Share this answer
 
Comments
Bharthish Athreya 22-Oct-15 5:38am    
I have used the same, ise is object of iselibraryentities. Remove option after entity.tablename.) is not possible, wonder why?, I have mentioned the error also in my first(question)post. That didn't work, Thank you.
GOT SOLUTION here it is....


C#
private void button3_Click(object sender, EventArgs e)
       {
           var selectedidlist = new List<string>();
           for (var rownum = 0; rownum < dataGridView1.Rows.Count; rownum++)
           {
               if (dataGridView1.Rows[rownum].Cells["Column1"].Value!=null && ((bool)(dataGridView1.Rows[rownum].Cells["Column1"]).Value))
               {
                   selectedidlist.Add(dataGridView1.Rows[rownum].Cells["USN"].Value.ToString());

               }
           }

           if (selectedidlist.Count > 0)
           {


               var selectedListString = selectedidlist.Aggregate((current, next) => current + ", " + next);

               String str = "delete  from T_BOOK_ISSUE_TABLE where USN in (@search)";
               SqlCommand xp = new SqlCommand(str, vid);
               xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = selectedListString;

               vid.Open();
               xp.ExecuteNonQuery();
               vid.Close();
               Delete_Load(null, null);
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900