Hi I'm working in VS with ASP.NET and razor, trying to fill a table with values from a db table but I have to decode or parse Json to simple text. I really appreciate some help. This is what i´m getting.
[HttpGet]
public ActionResult GetData()
{
string stdb = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
SqlConnection conn = new SqlConnection(stdb);
string sql = "SELECT *FROM[DB_PCC].[dbo].[Departments]";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader rd = cmd.ExecuteReader();
var st = "kyo please help me u.u";
return Json(new { success = true, message = rd },
JsonRequestBehavior.AllowGet);
}
<div id="result"></div>
<input type="button" name="name" value="try" onclick="DepListQuery()" />
<script>
function DepListQuery() {
$.ajax({
type: 'GET',
url: '@Url.Action("GetData","Home")',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('#result').text(response.message);
},
failure: function (response) {
alert("something get wrong u.u");
}
});
}
</script>
ExecuteReader()
and convert to a model and return a collection of that model, then loop through the collection in your script.SqlDataReader rd = cmd.ExecuteReader();
You then setmessage = rd
. You need to dord.Read()
to get the strings.