Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Delete a row using DeleteCommand of DataAdapter?

How to Delete a row using DeleteCommand of DataAdapter?

Scheduled Pinned Locked Moved C#
databasesysadminsecurityhelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gr8tushar
    wrote on last edited by
    #1

    Hii i m trying to delete a row using DeleteCommand of DataAdapter, but i can't. i m using this code.. using System; using System.Data; using System.Data.SqlClient; class ModifyDataTable { static void Main() { string constr = @"server=(local)\NetSDK;Integrated Security=true;"+ "database=address"; SqlConnection Conn = new SqlConnection(constr); try { string SQL = "SELECT * from Hello"; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(SQL,Conn); DataSet ds = new DataSet(); da.Fill(ds,"Hello"); DataTable dt = ds.Tables ["Hello"]; ("Select * From Hello",Conn); foreach( DataRow r in dt.Rows) { Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15), r["Phone"].ToString ().PadLeft(25)); } Console.WriteLine("\n" + dt.Rows .Count.ToString ()); da.DeleteCommand = new SqlCommand ("DELETE FROM Hello WHERE (Name='Amit')" ,Conn); da.Update (ds,"Hello"); Console.WriteLine ("\n" + dt.Rows.Count.ToString ()); foreach( DataRow r in dt.Rows) { Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15), r["Phone"].ToString ().PadLeft(25)); } } catch (Exception e) { Console.WriteLine ("Error Occured : " +e.Message ); Conn.Close (); } finally { Conn.Close(); Console.ReadLine (); } } } This isn't deleting anything neither in the dataset nor in the database

    C 1 Reply Last reply
    0
    • G gr8tushar

      Hii i m trying to delete a row using DeleteCommand of DataAdapter, but i can't. i m using this code.. using System; using System.Data; using System.Data.SqlClient; class ModifyDataTable { static void Main() { string constr = @"server=(local)\NetSDK;Integrated Security=true;"+ "database=address"; SqlConnection Conn = new SqlConnection(constr); try { string SQL = "SELECT * from Hello"; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(SQL,Conn); DataSet ds = new DataSet(); da.Fill(ds,"Hello"); DataTable dt = ds.Tables ["Hello"]; ("Select * From Hello",Conn); foreach( DataRow r in dt.Rows) { Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15), r["Phone"].ToString ().PadLeft(25)); } Console.WriteLine("\n" + dt.Rows .Count.ToString ()); da.DeleteCommand = new SqlCommand ("DELETE FROM Hello WHERE (Name='Amit')" ,Conn); da.Update (ds,"Hello"); Console.WriteLine ("\n" + dt.Rows.Count.ToString ()); foreach( DataRow r in dt.Rows) { Console.WriteLine("{0} {1}",r["Name"].ToString ().PadRight (15), r["Phone"].ToString ().PadLeft(25)); } } catch (Exception e) { Console.WriteLine ("Error Occured : " +e.Message ); Conn.Close (); } finally { Conn.Close(); Console.ReadLine (); } } } This isn't deleting anything neither in the dataset nor in the database

      C Offline
      C Offline
      Charlie Williams
      wrote on last edited by
      #2

      You should use a DataAdapter when you want your updates and deletes to be managed for you depending on the state of the DataSet. In your case, you are not altering the DataSet, so your delete command is never used. If you just want to execute a specific delete command:

      string sql = "DELETE FROM Hello WHERE (Name='Amit');
      SqlCommand comm = new SqlCommand(sql, conn);
      conn.Open();
      comm.ExecuteNonQuery();
      conn.Close();

      Charlie if(!curlies){ return; }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups