Issue With Restful Webservice +json+sql Stored Procedure Project
Solution 1:
OK, so there are several things that you need to change to make it working:
Add parameterless constructor to Note as it will be needed for deserialization:
publicNote() { }Get rid of "static" in Note's fields:
public
staticstring Client { get; set; }public
staticint Case { get; set; }public
staticstring Text { get; set; }public
staticint NoteId { get; set; }public
staticstring R1 { get; set; }public
staticstring R2 { get; set; }public
staticstring S1 { get; set; }public
staticDateTime Date { get; set; }public
staticbool Type { get; set; }Don't send JSON array if you want just 1 object, it won't deserialize. You are expecting single object, not array, so don't send array.
You have Type as bool, but you are sending string "1", this will not deserialize to true value as you might expected. Either send true/false (not "true"/"false") or change type of Type to string.
Get rid of that private item field, you don't need it:
private Note item;Get rid of those constructors that you have there
public Note(string json)public Note(Note item)Not only that they make no sense and won't work, you don't need them as JSON deserializer will fill the fields for you.
EDIT: For example, you say it does not build because there is no more a constructor with one parameter. Of course it does not build, there is this line
Notenotesdata=newNote(item);
but you do not need that line. What is the idea behind this line? You want an instance of Note class, but you already have it in "item" variable. You do not need to create a second copy of that. So get rid of this too.
Another reason, why it won't compile is that you get rid of those static fields, while you still have this in your Add method:
cmd.Parameters.Add("@Text", SqlDbType.VarChar).Value = Note.Text.Trim();
cmd.Parameters.Add("@When", SqlDbType.DateTime).Value = Note.Date;
and I am quite sure you do not want that. Instead, you want to use the instance of the object that were sent to you:
cmd.Parameters.Add("@Text", SqlDbType.VarChar).Value = item.Text.Trim();
cmd.Parameters.Add("@When", SqlDbType.DateTime).Value = item.Date;
Another thing is that there is usually no reason why would Add method return the object being added to DB. So feel free to change this
public Note Add(Note item)to this
publicvoidAdd(Note item)and do not return anything, you do not need it.
I am no expert on SqlConnection and things around it, so that part I do not comment. I use EF in my projects for working with DB. So there might be some problems in that part, but I can't comment on that.
Post a Comment for "Issue With Restful Webservice +json+sql Stored Procedure Project"