I’m not very good with ajax but I am trying to call a method in the code behind to cehck if a stored procedure is returning no data or returning data, and if it is returning data, then make the method return a bool evaluating to true. I am passing in a list of id’s to the method.
However my ajax call is probably wrong.
Here is my ajax:
var hasExhibitLinked = false;
var selectedTasksList = getSelectedTaskIDs();
$.ajax({
type: "POST",
url: '<%=ResolveUrl("~/Tasks/ViewTasks.aspx/HasExhibitLinked")%>',
data: "{'taskID':['" + selectedTasksList.join(',') + "']}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
arr = data.d;
hasExhibitLinked = arr[0];
},
error: function (data) {
}
});
if (hasExhibitLinked) {
showMessage("There is an Exhibit linked.");
}
else {
showMessage("Not exhibits linked");
}
here is my code behind if more information is needed:
EDIT:
[WebMethod]
public static bool[] HasExhibitLinked(String[] taskID)
{
bool hasLink = false;
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
var cmd = new SqlCommand("p_Link_List", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Taskid", SqlDbType.Int));
foreach (var id in taskID)
{
cmd.Parameters["@Taskid"].Value = taskID;
try
{
conn.Open();
String s = (String)cmd.ExecuteScalar();
if (s != null)
hasLink = true;
}
catch (SqlException sql)
{
ErrorLogger.Log(sql.Number, sql.Source, sql.Message);
}
catch (Exception ex)
{
ErrorLogger.Log(ex);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
return new bool[] { hasLink };
}
taskID is an
intin the code behind and anArrayin the JavaScript.would require a method signature like:
Question Two in comments: This is a common mistake with ajax. That code is asynchronous. Therefore that variable doesn’t exist when you are trying to access it. This is why you see a lot of “callback” functions.