I've got a custom new item form which I'm using in SharePoint 2007. What I want to happen is that once the user has entered an asset id and they leave the field, an ajax query checks to see if that asset id is available.
I've used the following code successfully on a normal .NET web applicaiton, however when using the same code in SharePoint, all I get returned is an Internal Server Error and the IIS logs record a status code of 500.
JQuery in new asset form
$(document).ready(function() {
$('#ctl00_PlaceHolderMain_newAsset_assetId').blur(function() {
assetId = $(this).val();
if (assetId.length >= 3) {
$.ajax({
type: "POST",
url: "checkAsset.aspx/AssetIdAvailable",
data: "{'assetId':'" + assetId + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(message) {
if (message.d != true) {
//$('#ctl00_PlaceHolderMain_saveNew').attr('disabled', 'disabled');
alert('This asset id is available');
}
else {
alert('This asset id is NOT available');
//$('#ctl00_PlaceHolderMain_saveNew').removeAttr('disabled');
}
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.statusText);
}
});
}
});
});
Code behind
public class CheckAssetAvailable : Page
{
[WebMethod]
public static bool AssetIdAvailable(string assetId)
{
int recCount = 0;
bool available = false;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ICTAssetDb"].ConnectionString);
SqlCommand comm = new SqlCommand("usp_IsAssetIdAvailable", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("@AssetId", SqlDbType.NVarChar).Value = assetId;
conn.Open();
recCount = int.Parse(comm.ExecuteScalar().ToString());
if (recCount == 0)
{
available = true;
}
comm.Dispose();
conn.Close();
conn.Dispose();
return available;
}
}