Reputation: 81
I want to delete a record from a datagridview that's in one form by pressing a button in another form. But I am getting a nullreferenceexception was unhandled
error. I am new to c# so if someone could write me the correct code I would really appreciate it.
Here's what I got so far.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewRow dr = dataGridView1.Rows[i];
if (dr.Selected == true)
{
dataGridView1.Rows.RemoveAt(i);
try
{
con.Open();
cmd.CommandText = "Delete from motociclete where codm=" + i + "";
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
this.Close();
}
Upvotes: 0
Views: 2507
Reputation: 3833
I see that you are trying to access the grid of one form from another. Make sure that you get the reference of your form (the one with the grid) properly and refer the grid using that. to achieve that you may have to expose your grid object as public.
In your parent form expose a property for the grid
public GridView Grid
{
return dataGridView1;
}
..and when you launch the new form (say ChildForm) do like this
Form child = new ChildForm(this);
child.Show();
Please make changes to your child form to use the constructor argument.
private Form m_ParentForm;
public ChildForm(Form child)
{
m_ParentForm = child;
}
..and your loop would look like this
for (int i = 0; i < m_ParentForm.Grid.Rows.Count; i++)
Hope this helps.
Upvotes: 0
Reputation: 159
Why do you loop through all rows, use dataGridView1.SelectedRows:
for (int i = dataGridView1.SelectedRows.Count - 1; i >= 0; i--)
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[i]);
Also bind your data by using a DataTable and BindingSource.
Upvotes: 0
Reputation: 600
private void button1_Click(object sender, EventArgs e)
{ int row =-1;
SqlConnection con = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
row = new_tab_Object.CurrentCell.RowIndex;
if (row!= (-1))
{
new_tab_Object.Rows.RemoveAt(row);
row = -1;
try
{
con.Open();
cmd.CommandText = "Delete from motociclete where codm=" + row + "";
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
this.Close();
}
thy this...
Upvotes: 0
Reputation: 216293
Just invert the verse of the loop.
for (int i = dataGridView1.Rows.Count - 1; i >= 0 ; i--)
In this way your loop is not affected by the changing number of rows
Also. This is the case where I would not open/close the connection at every command execution, and the command execution could be more performant if you use a parameter in this way
using(SqlConnection con = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True"))
using(SqlCommand cmd = new SqlCommand("Delete from motociclete where codm=@id", con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", 0);
for (int i = dataGridView1.Rows.Count-1; i >= 0; i++)
{
DataGridViewRow dr = dataGridView1.Rows[i];
if (dr.Selected == true)
{
dataGridView1.Rows.RemoveAt(i);
cmd.Parameters["@id"].Value = i;
cmd.ExecuteNonQuery();
}
}
}
Upvotes: 1
Reputation: 17600
You are removing rows while iterating over them and that causes NullPointerException
because when you remove row, count is changing but loop is still going for as long as initial count.
One way of doing it is creating temporary list of rows and delete them after:
List<DataGridViewRow> rowstodelete = new List<DataGridViewRow>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewRow dr = dataGridView1.Rows[i];
if (dr.Selected)
{
rowstodelete.Add(dr);
try
{
con.Open();
cmd.CommandText = "Delete from motociclete where codm=" + i + "";
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
foreach (DataGridViewRow row in rowstodelete)
{
dataGridView1.Rows.Remove(row);
}
Upvotes: 0