The task is to develop SL based application for inserting, updating, deleting data in any table of MS SQL database. As far as I know I can't use DomainService because I don't have any knowledge about tables ( I can get it in runtime but not in designtime). Can anybody advice me, please, how can I bind data to DataGrid (for example) and make it editable. I read about ObservableCollection but still don't understand how to apply changes in collection to DB.
Thanks in advance
Answer: 1
the simple way to manage data with SL is using Ria Services, but the latter are based on Entity Framework, so you must have an Entity model...
Else, you can build a specific logic with a simple Web service. The latter can open a database using any ADO.NET stuff and can send table lists, fields etc and data themselves. The hard part (not so hard) is on server side and will benefit the full framework, the SL app will just call the web service and display data (with some more options you'll be able to edit or delete data).
by :
Answer: 2
Can you give a link to example of such kind of service (server side), please?. I can cope with data representation but don't know how to write a service
by :Answer: 3
Take a look at some of the videos under the Learn tab above.
by :Answer: 4
LoiIver
Can you give a link to example of such kind of service (server side), please?. I can cope with data representation but don't know how to write a service
There is no direct sample that's showing what you want, but you can try some of the resources of this web site ("Learn" and "Samples" can help you on some points).
Basically you just need a simple web service, an ASMX can do perfectly the job. The methods will be "OpenDB(dnname)" returning true/false, CloseDB(), the same, ReturnListOfTables() that will return a List<string> of all tables in the db, and so on.
On Silverlight side, once the service will be ok (very simple to test calling it directly in IE without any SL app), you'll just have to add the service reference and call methods.
by :Answer: 5
odahan
On Silverlight side, once the service will be ok (very simple to test calling it directly in IE without any SL app), you'll just have to add the service reference and call methods.
odahan, I 've watched several videos here in learning part, wrote a service and got test string from it in sl client. The only thing I didn't understand is how I can work with data from different tables. Well, I know their names, their attributes, I can return a collection of rows of each table, make it an itemssource for datagrid, generate columns in grid automatically, but how can I edit data in this collection and apply it to db?
by :Answer: 6
mtiede@swtec...
Thanks for advice, I followed it
by :Answer: 7
You made a "get list" service to fetch the data to the client as objects. Now you just need an "update list" type service and send it the objects that changed. I usually do something like make each object have an IsChanged property. I put an IPropertyChanged listener on each object and set the IsChanged when some property changes. Then I can update the database from the objects that have changed.
There is a bunch of stuff you can do with RIA services, but I find them too "heavyweight" and complicated myself. But you might find them helpful.
There are also things you can do with the Entity Framework and Code First, for instance.
But the simplest to understand, I think, is to just use plain objects with a service that will do the update. (or insert or delete as appropriate)
There is also ObservableCollection type that you might want to read about as well as IEditableObject interface.
by :Answer: 8
mtiede@swtechnologies.com
There is also ObservableCollection type that you might want to read about as well as IEditableObject interface.
mtiede,
ObservableCollection of what should I create for retrieving data from DB? RiaServices create special class for every table and how can I create something with unknown structure?
I can build a "select" query for every table but can't understand, how can I put results into ObservableCollection.
by :Answer: 9
Okay, you didn't say you were using RIA services. I find them too complicated, but that is just me.
There are videos to watch on RIA services. I suspect they cover the updating.
RIA gives you some mechanism so that you can bind your data to the datagrid itemssource. Assuming you have already done that and see data, changes to the values in the datagrid automatically update the objects they are bound to.
Then to persist these changes, I suspect you do something like SubmitChanges in a button click event.
I ignore RIA. Too much to learn, for me.
I just make a service that returns a List<Person>, for instance. Then on the client, I make an ObservableCollection from the List. I bind the collection to the datagrid itemssource and edit away.
When I want to save it, I make another service that takes a List<Person> and I pass that service the list of modified objects. Then the service processes that list and updates the records.
I'm sure RIA is fine if you figure out all its twists and turns. And those videos probably show how to do all that.
Do you watch these:
http://www.silverlight.net/learn/advanced-techniques/wcf-ria-services
by :Answer: 10
The task is to edit ANY table that ois why I can't use RIA and I've mentioned it only as an example. The main difficulty is that I should develop an app, than can get information from ANY table so I don't know the structure of table in designtime.
You said before, that I should use ObservableCollection of object.
And the problem is to fill this collection and to bind it to datagrid in sl-client.
ObservableCollection<object> result = new ObservableCollection<object>(); try { OpenDB(); if (con.State == System.Data.ConnectionState.Open) { string s = ""; ... // now string s contains attribute names comma separated SqlCommand com1 = new SqlCommand("select " +s +" from "+ tablename, con); SqlDataReader reader1 = com1.ExecuteReader(); while (reader1.Read()) { for (int j = 0; j < reader1.FieldCount; j++) { WHAT SHOULD I DO HERE? } result.Add(?????); } reader1.Close(); } } finally { CloseDB(); } return result; }
Answer: 11
mtiede@swtechnologies.com
But the simplest to understand, I think, is to just use plain objects with a service that will do the update. (or insert or delete as appropriate)
What did you mean saying "plain objects" ? How can I work with objects? (I just can't imagine key words to find information in google)
by :Answer: 12
LoiIver
ObservableCollection<object> result = new ObservableCollection<object>(); try { OpenDB(); if (con.State == System.Data.ConnectionState.Open) { string s = ""; ... // now string s contains attribute names comma separated SqlCommand com1 = new SqlCommand("select " +s +" from "+ tablename, con); SqlDataReader reader1 = com1.ExecuteReader(); while (reader1.Read()) { for (int j = 0; j < reader1.FieldCount; j++) { WHAT SHOULD I DO HERE? } result.Add(?????); } reader1.Close(); } } finally { CloseDB(); } return result; }
The code you are showing is on the server side. The place where the binding and observablecollection go are on the client side. I make the service return a List of objects. Then on the client side, I use Linq and make an ObservableCollection from the list. This collection is then used as the itemssource of the datagrid.
I don't know what goes in the "What goes here" part. I don't really know what you want to do. I never do the kind of thing you are doing.
In fact, what you have so far is very dangerous. Assuming you are intending on passing the "s" variable from the client and somehow setting that on the client, that means that you have a source of "sql injection". If you google or bing on that, I suspect you will find a lot.
So, in general, I would recommend NEVER doing what you are doing. Unless this is not a general tool for a bunch of users. And even then, if you really are going to allow such a thing, then you should do some checking on the server that they aren't doing things that would cause injection. And it is relatively hard to make something that will catch EVERYTHING some hacker might try.
It is much better to use parameters in the sql. That way injection can't happen.
Now back to your problem.
Maybe you want your object to be a List of "fields". And each Field could have a Type and a Value. So you could get that information and store the type and value for each "field" and make your returned List be a list of those field lists.
So what you are doing is trying to make something very generic. That is going to be a lot of work. Since I don't do that sort of thing, I don't have any good suggestions for you.
I make explicit services that give back explicit results so that types and values can be checked by the compiler and the UI automatically.
good luck.
by :Answer: 13
..
by :Answer: 14
mtiede@swtechnologies.com
I make explicit services that give back explicit results so that types and values can be checked by the compiler and the UI automatically.
good luck.
thank you very much for your answer, i understood about sql-injections and i need some time to think about other part of your answer
by :
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog