Thread: Updating A Database From A Web Service

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    20

    Updating A Database From A Web Service

    I have this code in a web service, but when I try to update the database using the second method (setContacts), it does not update at all, although I do not get an error message.

    Code:
    [WebMethod] 
    public DataSet getContacts() 
    { 
       using (SqlConnection con = new SqlConnection(connString)) 
       {  
          DataSet ds = new DataSet("Contacts"); 
          SqlCommandBuilder cb = new SqlCommandBuilder(da); 
          da = new SqlDataAdapter("SELECT * FROM Contacts", con); 
          da.Fill(ds); 
         return ds; 
       }; 
    } 
    
    [WebMethod] 
    public void setContacts(DataSet data) 
    { 
       using (SqlConnection con = new SqlConnection(connString)) 
       { 
          DataSet ds = new DataSet("Contacts"); 
          SqlCommandBuilder cb = new SqlCommandBuilder(da); 
         da = new SqlDataAdapter("SELECT * FROM Contacts", con); 
        da.Update(data, "Contacts"); 
       }; 
    }

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Quote Originally Posted by enkwiringmindz View Post
    I have this code in a web service, but when I try to update the database using the second method (setContacts), it does not update at all, although I do not get an error message.

    Code:
    [WebMethod] 
    public DataSet getContacts() 
    { 
       using (SqlConnection con = new SqlConnection(connString)) 
       {  
          DataSet ds = new DataSet("Contacts"); 
          SqlCommandBuilder cb = new SqlCommandBuilder(da); 
          da = new SqlDataAdapter("SELECT * FROM Contacts", con); 
          da.Fill(ds); 
         return ds; 
       }; 
    } 
    
    [WebMethod] 
    public void setContacts(DataSet data) 
    { 
       using (SqlConnection con = new SqlConnection(connString)) 
       { 
          DataSet ds = new DataSet("Contacts"); 
          SqlCommandBuilder cb = new SqlCommandBuilder(da); 
         da = new SqlDataAdapter("SELECT * FROM Contacts", con); 
        da.Update(data, "Contacts"); 
       }; 
    }
    First of all, your SqlCommandBuilder is not going to do anything because there is no instance of da when this line runs. So those two lines need to be reversed. Check here (SqlCommandBuilder Class (System.Data.SqlClient)) to set up the command builder to properly create commands for you.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    I meant to ask also. What are you planning on doing with the DataSet ds? Right now, it has no tables and is not used. So i'm wondering if you are getting confused with "Contacts" and what should be updated. The link I gave you should help explain some things.
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-07-2008, 07:48 AM
  2. offline web service programming?
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 12-11-2006, 02:50 AM
  3. web based database
    By xddxogm3 in forum Tech Board
    Replies: 5
    Last Post: 04-20-2006, 01:55 PM
  4. Consuming same Web Service multiple times
    By cfriend in forum C# Programming
    Replies: 2
    Last Post: 01-10-2006, 09:59 AM

Tags for this Thread