I am using this method to return a XML as result. I need to return a json object after executing the stored procedure. Where should I edit the following code to return a JSON object?
public XmlElement GetGraphData(int eventTypeID, int patientID)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("sp_GetGraphData", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@EventID", eventTypeID);
cmd.Parameters.AddWithValue("@PatientID", patientID);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
XmlDataDocument xmldata = new XmlDataDocument(ds);
XmlElement xmlElement = xmldata.DocumentElement;
}
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!ExecuteNonQuery, and a second time onda.Fill(ds)- don't do that! What are you trying to do? Load data from the database? Don't useExecuteNonQueryfor that! This call is only intended for SQL statements that don't return any data! (LikeINSERT,DELETE)Newtonsofts Json.Net