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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. code for updating datatable.

code for updating datatable.

Scheduled Pinned Locked Moved C#
databasecomsaleshelpannouncement
10 Posts 5 Posters 1 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.
  • O Offline
    O Offline
    Omar Akhtar Sheikh
    wrote on last edited by
    #1

    // please anyone could help me write the update code public DataTable GetCustomers()                   {                            string query = "SELECT * FROM Customer_2";                            SqlDataAdapter da = new SqlDataAdapter(query, constr);                            DataTable table = new DataTable();                            da.Fill(table);                            return table;                   }                   // For Inserting Customers                   public void InsertCustomer(string customerName)                   {                            string query = "INSERT INTO Customer_2 (CustomerName) VALUES (@CustomerName)";                            SqlConnection con = new SqlConnection(constr);                            SqlCommand com = new SqlCommand(query, con);                            com.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = customerName;                            con.Open();                            com.ExecuteNonQuery();                            con.Close();                   }       &nbs

    N P A 3 Replies Last reply
    0
    • O Omar Akhtar Sheikh

      // please anyone could help me write the update code public DataTable GetCustomers()                   {                            string query = "SELECT * FROM Customer_2";                            SqlDataAdapter da = new SqlDataAdapter(query, constr);                            DataTable table = new DataTable();                            da.Fill(table);                            return table;                   }                   // For Inserting Customers                   public void InsertCustomer(string customerName)                   {                            string query = "INSERT INTO Customer_2 (CustomerName) VALUES (@CustomerName)";                            SqlConnection con = new SqlConnection(constr);                            SqlCommand com = new SqlCommand(query, con);                            com.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = customerName;                            con.Open();                            com.ExecuteNonQuery();                            con.Close();                   }       &nbs

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Write what update code? The C# or SQL? How about this[^]?


      only two letters away from being an asset

      N 1 Reply Last reply
      0
      • N Not Active

        Write what update code? The C# or SQL? How about this[^]?


        only two letters away from being an asset

        N Offline
        N Offline
        Nagy Vilmos
        wrote on last edited by
        #3

        Wow! ur so rad man! i'd nvr thnk 2 bing! :cool: [NOTE THE JOKE ICON - the norm is to use google!]


        Panic, Chaos, Destruction. My work here is done.

        P 1 Reply Last reply
        0
        • O Omar Akhtar Sheikh

          // please anyone could help me write the update code public DataTable GetCustomers()                   {                            string query = "SELECT * FROM Customer_2";                            SqlDataAdapter da = new SqlDataAdapter(query, constr);                            DataTable table = new DataTable();                            da.Fill(table);                            return table;                   }                   // For Inserting Customers                   public void InsertCustomer(string customerName)                   {                            string query = "INSERT INTO Customer_2 (CustomerName) VALUES (@CustomerName)";                            SqlConnection con = new SqlConnection(constr);                            SqlCommand com = new SqlCommand(query, con);                            com.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = customerName;                            con.Open();                            com.ExecuteNonQuery();                            con.Close();                   }       &nbs

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Not that I use it myself, but: DataAdapter.Update

          1 Reply Last reply
          0
          • N Nagy Vilmos

            Wow! ur so rad man! i'd nvr thnk 2 bing! :cool: [NOTE THE JOKE ICON - the norm is to use google!]


            Panic, Chaos, Destruction. My work here is done.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Never heard of them.

            1 Reply Last reply
            0
            • O Omar Akhtar Sheikh

              // please anyone could help me write the update code public DataTable GetCustomers()                   {                            string query = "SELECT * FROM Customer_2";                            SqlDataAdapter da = new SqlDataAdapter(query, constr);                            DataTable table = new DataTable();                            da.Fill(table);                            return table;                   }                   // For Inserting Customers                   public void InsertCustomer(string customerName)                   {                            string query = "INSERT INTO Customer_2 (CustomerName) VALUES (@CustomerName)";                            SqlConnection con = new SqlConnection(constr);                            SqlCommand com = new SqlCommand(query, con);                            com.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = customerName;                            con.Open();                            com.ExecuteNonQuery();                            con.Close();                   }       &nbs

              A Offline
              A Offline
              Adam R Harris
              wrote on last edited by
              #6

              Your using your DataAdapter all wrong. It should be declared outside the scope of the GetCustomers(), so it can be accessed by all the other functions, and you should be using it to Create/Read/Updated/Delete. How To Update a SQL Server Database by Using the SqlDataAdapter Object in Visual C# .NET[^] NOTE: the code below has not been tested / probably wont compile because i cant spell your code should look something like this;

                            // Dont know if this is going to be accessed outside of the class
                            // If so you might want to make a wrapper property for it
                            private SqlDataAdapter da;
                            // Should contain your data
                            private DataSet ds;
              
                            ..... somewhere in your initialization code .....
                            da = new SqlDataAdapter("SELECT \* FROM Customer\_2", \[YourConnectionString\]);
                            da.UpdateCommand = \[your update command\];
                            da.DeleteCommand = \[your delete command\];
                            da.InsertCommand = \[your insert command\];
              
                            // dont return anything here, just fill the dataset
                            public void GetCustomers()
                            {
                                     da.Fill(ds);
                            }
              
                            // Saves all the changes to the database
                            public void SaveChanges()
                            {
                                     da.Update(ds, "\[your table name\]"); 
                                     // Accept the changes in the dataset
                                     ds.AcceptChanges(); 
                            }
              

              Then all you need to do is Create/Update/Delete all the rows you want in the DataSet, just make sure you call SaveChanges(). Thats how your supposed to use the DataAdapters. But if you still want to use it in the way that you are using it, your going to have to loop through all the rows in the DataTable and check if their RowState == DataRowState.Changed (i think, could be updated), then just run your update command against SQL.

              If at first you don't succeed ... post it on The Code Project and Pray.

              O 1 Reply Last reply
              0
              • A Adam R Harris

                Your using your DataAdapter all wrong. It should be declared outside the scope of the GetCustomers(), so it can be accessed by all the other functions, and you should be using it to Create/Read/Updated/Delete. How To Update a SQL Server Database by Using the SqlDataAdapter Object in Visual C# .NET[^] NOTE: the code below has not been tested / probably wont compile because i cant spell your code should look something like this;

                              // Dont know if this is going to be accessed outside of the class
                              // If so you might want to make a wrapper property for it
                              private SqlDataAdapter da;
                              // Should contain your data
                              private DataSet ds;
                
                              ..... somewhere in your initialization code .....
                              da = new SqlDataAdapter("SELECT \* FROM Customer\_2", \[YourConnectionString\]);
                              da.UpdateCommand = \[your update command\];
                              da.DeleteCommand = \[your delete command\];
                              da.InsertCommand = \[your insert command\];
                
                              // dont return anything here, just fill the dataset
                              public void GetCustomers()
                              {
                                       da.Fill(ds);
                              }
                
                              // Saves all the changes to the database
                              public void SaveChanges()
                              {
                                       da.Update(ds, "\[your table name\]"); 
                                       // Accept the changes in the dataset
                                       ds.AcceptChanges(); 
                              }
                

                Then all you need to do is Create/Update/Delete all the rows you want in the DataSet, just make sure you call SaveChanges(). Thats how your supposed to use the DataAdapters. But if you still want to use it in the way that you are using it, your going to have to loop through all the rows in the DataTable and check if their RowState == DataRowState.Changed (i think, could be updated), then just run your update command against SQL.

                If at first you don't succeed ... post it on The Code Project and Pray.

                O Offline
                O Offline
                Omar Akhtar Sheikh
                wrote on last edited by
                #7

                Kindly would you please help me code the whole thing because it is urgent and I still want to do it my own way. Please help it is emergency, my daughter is in hospital, I have loads of tension on my mind due to her. <code> // Dont know if this is going to be accessed outside of the class                            // If so you might want to make a wrapper property for it                            private SqlDataAdapter da;                            // Should contain your data                            private DataSet ds;                            ..... somewhere in your initialization code .....                            da = new SqlDataAdapter("SELECT * FROM Customer_2", [YourConnectionString]);                            da.UpdateCommand = [your update command];                            da.DeleteCommand = [your delete command];                            da.InsertCommand = [your insert command];                            // dont return anything here, just fill the dataset                            public void GetCustomers()                            {                                        da.Fill(ds);                            }                &n

                A 1 Reply Last reply
                0
                • O Omar Akhtar Sheikh

                  Kindly would you please help me code the whole thing because it is urgent and I still want to do it my own way. Please help it is emergency, my daughter is in hospital, I have loads of tension on my mind due to her. <code> // Dont know if this is going to be accessed outside of the class                            // If so you might want to make a wrapper property for it                            private SqlDataAdapter da;                            // Should contain your data                            private DataSet ds;                            ..... somewhere in your initialization code .....                            da = new SqlDataAdapter("SELECT * FROM Customer_2", [YourConnectionString]);                            da.UpdateCommand = [your update command];                            da.DeleteCommand = [your delete command];                            da.InsertCommand = [your insert command];                            // dont return anything here, just fill the dataset                            public void GetCustomers()                            {                                        da.Fill(ds);                            }                &n

                  A Offline
                  A Offline
                  Adam R Harris
                  wrote on last edited by
                  #8

                  Sigh ... you are never EVER EVER going to get someone to write your project for you on here. Regardless of your situation, sorry to hear, we all have our own problems and trying to play on our hearts is probably just going to piss more people off than anything. I would "write" this code for you, but it seems Microsoft already has. Case in point[^]

                  If at first you don't succeed ... post it on The Code Project and Pray.

                  O 1 Reply Last reply
                  0
                  • A Adam R Harris

                    Sigh ... you are never EVER EVER going to get someone to write your project for you on here. Regardless of your situation, sorry to hear, we all have our own problems and trying to play on our hearts is probably just going to piss more people off than anything. I would "write" this code for you, but it seems Microsoft already has. Case in point[^]

                    If at first you don't succeed ... post it on The Code Project and Pray.

                    O Offline
                    O Offline
                    Omar Akhtar Sheikh
                    wrote on last edited by
                    #9

                    [Message Deleted]

                    A 1 Reply Last reply
                    0
                    • O Omar Akhtar Sheikh

                      [Message Deleted]

                      A Offline
                      A Offline
                      Adam R Harris
                      wrote on last edited by
                      #10

                      Go fuck yourself

                      If at first you don't succeed ... post it on The Code Project and Pray.

                      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