How to delete row from datagridview and update database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajinkya ragalwa
    New Member
    • Feb 2011
    • 14

    How to delete row from datagridview and update database?

    Hey guys,

    I have written a code that could delete selected row in data grid view. I gives lots of errors.

    Let me paste my code.

    Code:
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
    
                            DataGridViewRow dr = dataGridView1.Rows[i];
                            if (dr.Selected == true)
                            {
                                dataGridView1.Rows.RemoveAt(i);
    
    
                                cmd.CommandText = "Delete from transact1 where C_ID ='" + i + "'";
                                cmd.ExecuteNonQuery();
    
                                da.Update(ds, "transact1");
                                MessageBox.Show("Deleted");
                            }
                            
                            con1.Close();
                        }

    While deleting row it throws exception.
    "Connection property not initialized"

    I have tried opening connection in different ways but it didn't help either. So can anyone tell me what can possibly wrong with this code?

    Thanks
    Ajinkya
    Last edited by Niheel; Mar 5 '11, 08:22 PM.
  • Leito
    New Member
    • Apr 2010
    • 58

    #2
    What code do you use to open the connection to the database?

    If your database is hosted on an SQL server, you should use something like this:
    Code:
    String strConnection = "Data Source=SQLSERVERNAME; Initial Catalog=DATABASENAME; User Id=USERNAME; Password=USERPASSWORD;Connection Timeout=30;"
    SqlConnection cn = new SqlConnection(strConnection);
    cn.Open();

    Comment

    Working...