Click here to Skip to main content
14,980,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to resolve issue of length is null or undefined while setting Autocomplete Text Box in asp.net with c#. I am setting the autocomplete based on Database in a content page. I am getting the exception
"Microsoft JScript runtime error: 'd.length' is null or not an object" on entering data in the Textbox.
The textbox in which autocomplete required is designed as below:
ASP.NET
<div class="ui-widget">
Enter SFTP FileName: 
<asp:TextBox type="text" id="txtSearch" runat="server" class="autosuggest" >
</div>


What I have tried:

JavaScript
$(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "AutoTB.aspx/GetAutoCompleteData",
                    data: "{'filename':'" + document.getElementById('MainContent_txtSearch').value + "'}",
                    dataType: "json",
                    success: function (data) {
                          if (data.d.length > 0) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.split('/')[0],
                                    val: item.split('/')[1]
                                }
                            }));
                        }
                        else {
                            response([{ label: 'No Records Found', val: -1 }]);
                        }
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            },
            select: function (event, ui) {
                if (ui.item.val == -1) {
                    return false;
                }
                $('#lblUserId').text(ui.item.val);
            }
        });
    }
Posted
Updated 21-Sep-17 3:57am
v4
Comments
Richard MacCutchan 21-Sep-17 7:06am
   
Is d a property of data ?
ranio 21-Sep-17 7:12am
   
It is as per the code obtained.
Thanks7872 21-Sep-17 7:39am
   
What do you mean? Put debugger just after success and try to look what is there in data.
ranio 21-Sep-17 7:45am
   
Getting Authentication Failed as in data variable on setting the debugger over the script. Then on reaching data.d.length portion getting the exception length is null or undefined
ZurdoDev 21-Sep-17 8:06am
   
If authentication failed then data.d will be null.

1 solution

validate for null value
if(data!=undefined && data.d!=undefined)
if (data.d.length > 0) {
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900