Updating a DataSet Through a Webservice
Alright, so I’ve been racking my brain around this whole n-tier architecture and started converting one client’s software to this type of solution. When it comes to creating the various layers and web service that just pulls data it’s easy as pie. Updating a DataSet through a data access layer that communicates to a web service is a whole other story. Here’s a quick overview of how I achieved the update.
1) The smart client issued an update event that called the DAL (date access layer) update.
2) The DAL passes the DataSet to the web service. Keep in mind during the first two steps I used the generic type DataSet. The smart client doesn’t actually contain the DataSet types that are stored in regular applications that could made the update a whole lot easier, that’s the reason why I’m using the generic DataSet type.
3) The webservice uses the method below for updating the DataSet. The more reference material I read lately, I may end up changing the update method, I believe that I’m doing it partially incorrect even though it works perfectly.
[WebMethod(Description = “Updates the edited Job Log row.”)] public String update_tblJobLog(DataSet tblJobLogDS) { DataSets.tblJobLogDataSetTableAdapters.tblJobLogTableAdapter tblJobLogTableAdapter = new Company_Webservice.DataSets.tblJobLogDataSetTableAdapters.tblJobLogTableAdapter(); Company_Webservice.DataSets.tblJobLogDataSet typedtblJobLogDS = new Company_Webservice.DataSets.tblJobLogDataSet(); typedtblJobLogDS.Load(tblJobLogDS.Tables[“tblJobLog”].CreateDataReader(), LoadOption.OverwriteChanges, typedtblJobLogDS.tblJobLog); typedtblJobLogDS.tblJobLog.Rows[0].SetModified(); try { tblJobLogTableAdapter.Update(typedtblJobLogDS); return “Success”; } catch (Exception ex) { return ex.Message; } }
There’s actually a way I can just pass the rows I need by using the GetChanges method, then cycle through the loaded rows to update the RowState. This style of programming definately has its pros and cons, but I think in the long run it’s going to work wonders for scalability since this particular client is expanding like their is no tomorrow.
[edit]
Just googled the magic phrase and found out Microsoft actually has a code snippet that shows an easy way to update a dataset through a webservice. It figures I nearly make my brain bleed trying to get the update to process earlier, and Microsoft has the solution sitting right there. Sometimes all it takes is the magic search phrase…
[WebMethod] public DataSet GetCustomers() { SqlConnection con = new SqlConnection(“server=servername;uid=login; pwd=password;database=northwind”); SqlDataAdapter daCust = new SqlDataAdapter(“Select * From Customers”, con); DataSet ds = new DataSet(); daCust.Fill(ds, “Cust”); return ds; } [WebMethod] public DataSet UpdateCustomers(DataSet ds) { SqlConnection con = new SqlConnection(“server=servername;uid=login; pwd=password;database=northwind”); SqlDataAdapter daCust = new SqlDataAdapter(“Select * From Customers”, con); SqlCommandBuilder cbCust = new SqlCommandBuilder(daCust); daCust.Update(ds, “Cust”); return ds; }






