0

As some background I've come via VB6 then VB.Net and am now trying out Bootstrap/MVC with C# so excuse the queries... I've SO searched and been on YouTube as well but a struggling a little. I've my approach is wrong do tell me!

Aim: Stored procedure to run and, in this case, confirm if the username and password are correct. I'll be wanting to add in insert/edit to later Stored Procedures.

Issue: When I run the code on the website I get no error or result. In debug mode I get:

Unhandled exception at line 133, column 5 in http://localhost:49647/Account/Login

0x800a1391 - JavaScript runtime error: '$' is undefined

Thoughts I am not a JS programmer but would assume I'd need to add something to 'Dim' the value in some way? Looking at 0x800a1391 - JavaScript runtime error: 'jQuery' is undefined I might need to call something?

User.cs

using System;
using System.Data.SqlClient;
using System.Data;
using System.Configuration; 

namespace Login.BL
{
    public class User
    {
        public int UserID { get; set; }
        //[Required]
        public int Username { get; set; }
        //[Required]
        public int UserPassword { get; set; }

    }
    public class UserBusinessLogic
    {
        string conStr = ConfigurationManager.ConnectionStrings["myTaylorWoodrowSQL"].ConnectionString;
        public int CheckUserLogin(User User)
        {
            //Check the user is valid!!!!
            using (SqlConnection conObj = new SqlConnection(conStr))
            {
                SqlCommand comObj = new SqlCommand("retrieveUserByNameAndPassword", conObj);
                comObj.CommandType = CommandType.StoredProcedure;
                comObj.Parameters.Add(new SqlParameter("@Username", User.Username));
                comObj.Parameters.Add(new SqlParameter("@Password", User.UserPassword));
                conObj.Open();
                return Convert.ToInt32(comObj.ExecuteScalar());
            }
        }
    }
}

Extract from Login.cshtml I can post full code

<script> 
    $("#Login").click(function ()
    {
        var dataObject = { UserName: $("#UserName").val(), Password: $("#Password").val() };
        $.ajax({
            url:'@Url.Action("Login","User")',
            type:"POST",
            data: dataObject,
            datatype: "json",
            success: function(result)
            {
                if(result.toString()=="Success")
                {
                    alert(result);
                }
                else
                {
                    alert(result);
                }
            },
            error: function (result)
            {
                alert("Error");
            }
        });
    })
</script>
2
  • 1
    are you including jquery before your script? Commented Mar 2, 2016 at 12:15
  • Did you add a <script> tag for jquery, something like <script src='/scripts/jquery.min.js"></script>? Commented Mar 2, 2016 at 12:15

2 Answers 2

2

JavaScript runtime error: '$' is undefined

Means jquery is not loaded into your page. So if its a Default MVC application it would be loaded in the _Layout page. But I think you have written the login page without using the default layout, So you can add this line of code to your login page head section (if you are using a layout page then head section of this layout) and everything must be fine.

  @Scripts.Render("~/bundles/jquery")

The default MVC application will have the Jquery and validation related files bundled under this name ~/bundles/jqueryand this is how we use it, Else if its a empty application then you must create a new bundle, Or add only the reference of the jquery file to the page like this.

 <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
//note just drag and drop the file into the page and VS will do the work for you of putting the proper URL there.
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I had used the default MVC layout (or think I did!) but added both of the above and it appears to be working! Thanks
Is there a better and cleaner example out there I should use @Reddy ?
Don't add both, Jquery should be loaded only once. Else it might cause issues while using some plugin's or might effect your custom code etc.
@indofraiser If you are looking for a start in MVC then I think this is a good reference for you.. codeproject.com/Articles/866143/Learn-MVC-Project-in-days-Day
1

You appear not to have loaded the jquery library before running your script.

See https://learn.jquery.com/about-jquery/how-jquery-works/ for details on how to include it.

It's best if you get a local copy of jquery.js and then insert a line such as

<script src="jquery.js"></script>

before the script tag with your own code.

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.