PDA

View Full Version : EditGrid HttpProxy using .NET CRUD Service. Date Update Problem



daplume
18 Sep 2013, 9:52 AM
Hi Everyone,

I've nearly got my EditorGridPanel working but ran into problems on updates when my store and grid use date fields.

I'm using a .NET service and have found I can Read and Delete just fine.

I've been able to cobble together Updates by hacking together a return string but that has fallen apart now that I'm trying to update using a date field. Consequently, on return from an update my grid date field is blank. My text fields are updating fine on return so I'm nearly there.

I'm wondering if anyone can offer advice returning update records from .NET, especially those containing date values please?

It would be especially helpful if anyone were willing to expose an example of the .NET code that illustrates a CRUD update within a .NET service.

Thanks!

daplume
18 Sep 2013, 10:35 AM
OK, found a solution --- here is what I've done using a .NET .asmx service. Apologies in advance if you find some other lapses in logic such as the error handling.

My HttpProxy, JsonStore and Edit Grid are working now.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
public void update()
{
try
{
// http://stackoverflow.com/questions/3398926/how-to-retrieve-json-via-asp-net-context-request

Context.Response.ContentType = "application/json";
Context.Response.ContentEncoding = System.Text.Encoding.UTF8;

System.IO.Stream body = Context.Request.InputStream;
System.Text.Encoding encoding = Context.Request.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);

//Strip away the junk
string sRawJson = reader.ReadToEnd();
sRawJson = sRawJson.Replace("{\"d\":", "");
sRawJson = sRawJson.Replace("}}", "}");

int i = 0;

//sDateNoteRec allows ingestion of the Json date as a string.
//date fields in this class are STRING.
sDateNoteRec p = JsonHelper.JsonDeserialize<sDateNoteRec>(sRawJson);
string sReturnString;

try
{
string sCon = ConfigurationManager.AppSettings.Get("DbConnStr");
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = sCon;
sqlCon.Open();

SqlCommand sqlCmd = new SqlCommand("spCv_UpdateNote", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;

if (!bChkIsNull(p.nNoteId))
sqlCmd.Parameters.Add(new SqlParameter("@nNoteId", p.nNoteId));
else
{
Context.Response.Write("NULL NoteId not allowed");
return;
}

if (!bChkIsNull(p.nGisId))
sqlCmd.Parameters.Add(new SqlParameter("@nGisId", p.nGisId));

if (!bChkIsNull(p.sMadeBy))
sqlCmd.Parameters.Add(new SqlParameter("@sMadeBy", p.sMadeBy));

// Parse the date in string format to date format as SQL date parameter
if (!bChkIsNull(p.dtDate))
sqlCmd.Parameters.Add(new SqlParameter("@dtNoteDate", DateTime.Parse(p.dtDate)));

if (!bChkIsNull(p.sNotetext))
sqlCmd.Parameters.Add(new SqlParameter("@sNoteText", p.sNotetext));

if (!bChkIsNull(p.sFilename))
sqlCmd.Parameters.Add(new SqlParameter("@sFileName", p.sFilename));

if (!bChkIsNull(p.sFiletype))
sqlCmd.Parameters.Add(new SqlParameter("@sFileType", p.sFiletype));

if (!bChkIsNull(p.nFilelen))
sqlCmd.Parameters.Add(new SqlParameter("@nFileLen", p.nFilelen));


i = sqlCmd.ExecuteNonQuery();

}
catch (Exception ex2)
{
sReturnString = ex2.Message;
}

// Convert the date string based object to date format object:
NoteRec pNote = new NoteRec(p.nNoteId, p.nGisId, DateTime.Parse(p.dtDate), p.sMadeBy, p.sNotetext, p.sFilename, p.sFiletype, p.nFilelen);

// Serialize the new object
string sJsonNote = JsonHelper.JsonSerializer<NoteRec>(pNote);

//Build the return string:
sReturnString = "{\"success\":true,\"message\":\"OK\",\"d\":" + sJsonNote + "}";
Context.Response.Write(sReturnString);

}
catch (Exception ex)
{
SoapException se = new SoapException(ex.Message, SoapException.ClientFaultCode);
throw se;
}

}

Gary Schlosberg
20 Sep 2013, 6:54 AM
Glad to hear that everything is working for you, and thanks for sharing your solution back with the community.