0

here is the getter method in c# it gets the meanings of a word input by a user by a query and fills them in a data table afterwards a list of strings will be filled by a loop which will contain those meanings which is at last converted and return as json string :

             public static string  getmeanings(string word)
         {
    string cs = 
            ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

                        List<string> stringArr = new List<string>();

    using (SqlConnection con = new SqlConnection(cs))
    {

        con.Open();
        using (SqlDataAdapter rdr = new SqlDataAdapter("SELECT MEANING FROM WORDS T1 , MEANINGS T2 WHERE WORD LIKE N'" + word + "'AND T1.WORD_ID = T2.WORD_ID", con))
        {
            using (DataTable dt = new DataTable())
            {

                rdr.Fill(dt);
                for(int i =1;i< dt.Rows.Count; i++)
                {
                    stringArr.Add(dt.Rows[0][i].ToString());

                }

            }

            string json1 = JsonConvert.SerializeObject( stringArr);
            return json1;

        }
    }
}

here is the ajax/jquery code : on keyup the ajax will call the getter method and at success it will alter a table that contains the result of the return json object

       $(function () {

        $("#text1").keyup( function () {
            var word = $("#text1").val();
            $.ajax({
                type: "GET",
                url: "toshow.aspx/getmeanings",
                data: { word: word },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {




                $(".tablefill").append("<table><tr><td>meaning id</td><td>meaning</td></tr><tr><td>"+result[0]+"</td></tr></table>");
                    console.log(result);
                }, error: function (err) {
                    alert('ERROR');

                }
            });
        });
    });

after i test the code ajax is succeeding , no error or failure but the result is always an undefined object in console log

2 Answers 2

1
console.log(result.success);

Are you sure 'success' is a member of 'result'? Perhaps try see what console.log(result) does.

You can start from the browser and debug backwards.

Open your developer console (F12 for chrome), go to the Network tab and then let Ajax fire again.

Then you will most likely notice that the server replied with a 200 response, just no content.

Now you start and work from your returned result back to your query.

My bet would be that it could likely be that 0 rows were returned and that your loop didn't populate stringArr. You might get another error once your query returns some rows as I am a bit uncertain about the multidimensional array. Looks like you might be trying to loop through an equal amount of columns as you have rows.

I am certain you will fix the errors occurring in your Code Behind once they start showing up.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks , yes it returned a 200 , about the loop , the query that is written will return only one column so the data table will also only be of one column, i changed it and it is now : stringArr.Add(dt.Rows[i].ToString()); but it is still making the same problem
Can you step through your code and inspect the value of dt.Rows? And while you are stepping through the code, can you confirm the loop actually is entered?
1

The result should be an array of items from your database. result.success would always be undefined, there is no .success on your returned object.

On another topic, you'll want to take care of your SQL Injection problem. (https://en.wikipedia.org/wiki/SQL_injection)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.