void SendInfoAsJson(WinStreakResponse response) { string strJson = JsonConvert.SerializeObject(response); Response.ContentType = "application/json; charset=utf-8"; Response.Write(strJson); Response.End(); }
protected void Page_Load(object sender, EventArgs e) { AddWinStreak request; WinStreakResponse response = new WinStreakResponse(); response.error = String.Empty; // Need passed in store id and number of requested results. // 1. Deserialize the incoming Json. try { request = GetRequestInfo(); } catch (Exception ex) { response.error = ex.Message.ToString(); // Return the results as Json. SendInfoAsJson(response); return; } // Do stuff here. SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); try { connection.Open(); string sql = String.Format("INSERT into users (totalWins) VALUES (@val1)"); SqlCommand command = new SqlCommand(sql, connection); command.Parameters.AddWithValue("@val1", request.id); command.Parameters.AddWithValue("@val2", request.wins); command.ExecuteNonQuery(); } catch (Exception ex) { response.error = ex.Message.ToString(); } finally { if (connection.State == ConnectionState.Open) { connection.Close(); } } SendInfoAsJson(response); }