Israel

Israel

  • 705
  • 1.3k
  • 216.3k

Delete one or multiple selected rows usind Database

Jul 20 2018 4:10 AM
Hi,
 
I put whole my code to be clear. Then I would like to delete multiple selected rows.
But when I run my program and select one or more rows its shows a error message saying "object doesnt be converted DBNull em others types".
 
Its sturk on this line: where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.BindGrid();
}
private void BindGrid()
{
dataGridView1.DataSource = null;
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Test\\WindowsFormsApplication1\\Database1.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Test", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
}
// SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Test\\WindowsFormsApplication1\\Database1.mdf;Integrated Security=True");
//SqlCommand comm;
//string connstr = @"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Test\\WindowsFormsApplication1\\Database1.mdf;Integrated Security=True";
private void button1_Click(object sender, EventArgs e)
{
List<DataGridViewRow> selectedRows = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true //Sturks here
select row).ToList();
if (MessageBox.Show(string.Format("Do you want to delete {0} rows?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
foreach (DataGridViewRow row in selectedRows)
{
using (SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = C:\\Test\\WindowsFormsApplication1\\Database1.mdf; Integrated Security = True"))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM test WHERE ID = @ID", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", row.Cells["ID"].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
this.BindGrid();
}
//using (SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Test\\WindowsFormsApplication1\\Database1.mdf;Integrated Security=True")) //ADDED
}
private void Form1_Load(object sender, EventArgs e)
{
string connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Test\\WindowsFormsApplication1\\Database1.mdf;Integrated Security=True";
string sql = "SELECT * FROM test";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "test");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "test";
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.AliceBlue;
this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
}
}
}

Answers (9)