Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ASP.NET
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication10._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
    $(document).ready(function () {
        $("#userinput").on('input', function () {
            if ($(this).val().length >= 8) {
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/fillfields",
                    contentType: "application/json; charset=utf-8",
                    data: '{employeeID: "' + $("#userinput").val() + '"}',
                    dataType: "json"
                });
                $('#userinput').val("");
            }
        });
    });

    </script>
    <br>
    <div style="text-align: center">
        <br>
        <input autofocus="autofocus"  id="userinput" />
        <br>
        <hr>
    </div>


C#
using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication10
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [System.Web.Services.WebMethod]
        public static void fillfields(string employeeID)
        {
            string constr = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(constr))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("TMS_INSERT", connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@EMP_ID", employeeID);
                    command.ExecuteNonQuery();
                }
                connection.Close();
            }
        }
    }
}

why my code fail to call the c# function?code look fine but fail to work : (

What I have tried:

please help and guide me,tried look into broswer console but its blank
Posted
Updated 18-Oct-16 0:07am
Comments
Suvendu Shekhar Giri 13-Oct-16 3:10am    
What is the input you are passing while doing the test?
Note:
if ($(this).val().length >= 8) 
KyLim0211 13-Oct-16 3:11am    
userinput
ZurdoDev 13-Oct-16 7:58am    
1. Do not add comments to your own question. Reply to comments instead.
2. This is a very, very simple thing for you to fix, and in fact, only you can. We cannot run your code. So, put in some breakpoints and do some simple debugging. You'll find the answer faster than it took you to post this question.
F-ES Sitecore 13-Oct-16 4:08am    
First off check that the event actually fires, then check it calls the ajax method, then check the network tab of your browser tools to see if the call went out and if any errors occurred.
Vincent Maverick Durano 13-Oct-16 16:53pm    
Have you tried debugging your code to figure out what went wrong? Yes debug, debug and debug!

"_" is missing in the url
JavaScript
url: "_Default.aspx/fillfields",
 
Share this answer
 
Quote:
You are using This code which is wrong :-

data: '{employeeID: "' + $("#userinput").val() + '"}',

Correct code is here:-

data: "{employeeID: '" + $("#userinput").val() + "'}",

*Note:- You must watch the Quotes places.
Thank You
 
Share this answer
 

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