I created a dashboard with widgets awhile ago and I'm now revisiting how to persist the the widgets per user to the database. I thought I was on the right track, but I'm very new to JSON and I'm unable to accomplish what I need. I've researched this and tried multiple things but nothing has worked out so far. Here is my code..
javascript that creates item for update
function updateWidgetData() {
var items = [];
$('.column').each(function () {
var columnId = $(this).attr('id');
$('.dragbox', this).each(function (i) {
var collapsed = 0;
if ($(this).find('.dragbox-content').css('display') == "none")
collapsed = 1;
//Create Item object for current panel
var item = {
id: $(this).attr('id'),
collapsed: collapsed,
order: i,
column: columnId
};
//Push item object into items array
items.push(item);
});
});
//Assign items array to sortorder JSON variable
var sortorder = { items: items };
Now my goal is to pass the sortorder to be saved to the database... but I have this for testing..
var testData = '{ "Column1": "test1", "Column2": "test2"}'
$.ajax ({
url: "/Handlers/SaveWidgets.ashx",
type: "POST",
contentType: "application/json; charset=uft-8",
dataType: "json",
data: testData,
success: function (response) {
alert("Passed json");
},
error: function (error) {
alert("Failed passing json.");
}
});
Then in my handler..
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string column1 = (string)context.Request.Form["Column1"];
string column2 = (string)context.Request.Form["Column2"];
using (SqlConnection connCao = new SqlConnection(ConfigurationManager.ConnectionStrings["dboCao"].ConnectionString))
{
using(SqlCommand cmdWidget = new SqlCommand("INSERT INTO TestTable (Column1, Column2) VALUES (@column1, @column2)", connCao))
{
cmdWidget.Parameters.AddWithValue("@column1", column1);
cmdWidget.Parameters.AddWithValue("@column2", column2);
connCao.Open();
cmdWidget.ExecuteNonQuery();
connCao.Close();
}
}
}
but I'm getting that it's expecting parameters @column1, and @column2 which were never supplied. So clearly I'm missing how to do something and I'm unable to find what I'm missing on the google machine.
I have used this link here but this doesn't explain the greatest and a few things confused me.
I also found other links but nothing that explains what I'm trying to accomplish. Any help is greatly appreciated!