Hi, I'm trying to save data from HTML table to database using jquery to call a page method. However, jquery call fails with : 500 internal server error.. Any advice?
Below is the code:
SCRIPT:
$(document).ready(function() {
$("#save").click(function(event) {
var i = 0;
var inputs = new Array();
$("#tbNames").find("input").each(function() {
inputs[i] = [this.id, this.value];
i++;
});
var columnsCount = 2;
$.ajax({
type: "POST",
url: "Default2.aspx/Save",
contentType: "application/json; charset=utf-8",
data: "{inputs: inputs, columnsCount: columnsCount}",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
});
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
});
</script>
HTML:
Name
Task
Delete
<input id="save" type="button" value="Save data" />
and C# code of page method:
[WebMethod ()]
public static List<Hashtable> ParseJson(string[] array, int rowsCount)
{
var result = new List<Hashtable>();
List<string[]> list = new List<string[]>();
foreach (var item in array)
{
int comma = item.IndexOf(',');
list.Add(new string[] { item.Substring(0, comma), item.Substring(comma + 1) });
}
int i = 0;
while (i < list.Count)
{
var dict = new Hashtable();
int j = i + rowsCount;
for (; i < j; i++)
{
dict.Add(list[i][0], list[i][1]);
}
result.Add(dict);
}
return result;
}
[WebMethod()]
public static string Save(string[] inputs, int columnsCount)
{
List<Hashtable> r = ParseJson(inputs, columnsCount);
List<TestClass> listOfTasks = new List<TestClass>();
foreach (var item in r)
{
TestClass tc = new TestClass();
tc.name = (string)item["name"];
tc.task = (string)item["task"];
listOfTasks.Add(tc);
}
DBManager dbm = new DBManager();
string actionResult = dbm.SaveData(listOfTasks);
if (actionResult == String.Empty)
{
return "Saved!";
}
else
{
return actionResult;
}
}