Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:06:24+00:00 2026-06-18T11:06:24+00:00

I am building a Web API that will be inserting some values into a

  • 0

I am building a Web API that will be inserting some values into a Microsoft SQL Server database.

This APIs eventual usage will be in a JAVA Spring application.

I don’t want to put the “carriage before the horse,” so as a POC, I wanted to prove to myself first that I can call my Web API from a simple web page.

Essentially, my simple web page has some text input fields and when I click on a button, I validate the text fields and then create a JSON string out of them. Next I have an Ajax call that passes this JSON string to my controller. My controller in .NET land receives my request, but chokes with the following error –

An error has occurred.”,”ExceptionMessage”:”String or binary data would be truncated.

And the stack trace follows.

at System.Data.SqlClient.SqlInternalConnection.OnError… blah blah

I think the problem is with several of the columns that I am attempting to insert into.

They are of type VARCHAR(1) and have the NOT NULL property in SQL Server.

On my simple web page, I never created a text input for these values. Instead, I figured that I could stick those values in when I am doing the actual INSERT on the server. Incidentally, I am using the TableAdapter to handle all my database connections, etc…

Code that makes all this happen is as follows:

//JQUERY - collecting the input and generating the JSON
$('#pythonIsActive').is(':checked') ? activeCourse = 'Y' : activeCourse = 'N';  
        $('#pythonRequiresSecurityClearance').is(':checked') ? clearanceRequired = 'Y' : clearanceRequired = 'N';
        $('#pythonOfferedFall').is(':checked') ? fall = 'Y' : fall = 'N';
        $('#pythonOfferedWinter').is(':checked') ? winter = 'Y' : winter = 'N';
        $('#pythonOfferedSpring').is(':checked') ? spring = 'Y' : spring = 'N';
        $('#pythonOfferedSummer').is(':checked') ? summer = 'Y' : summer = 'N';
        $('#pythonIsTentative').is(':checked') ? tenativeCourse = 'Y' : tenativeCourse = 'N';

        var Course = {
            CourseID: $('#pythonCourseId').val(),
            CourseLongName: $('#pythonLongName').val(),
            IsActiveCourse: activeCourse,
            Description: $('#pythonCourseDescription').val(),
            DepartmentID: $('#pythonDepartmentId').val(),
            LabHours: $('#pythonLabHours').val(),
            LectureHours: $('#pythonLectureHours').val(),
            CourseShortName: $('#pythonShortName').val(),
            EffectiveYear: $('#pythonEffectiveYear').val(),
            EffectiveQuarter: $('#pythonEffectiveQuarter').val(),
            IsClearanceRequired: $('#pythonRequiresSecurityClearance').val(),
            ClearanceRequired: $('#pythonSecurityClearanceRequired').val(),
            CourseCoordinatorID: $('#pythonCoordinatorID').val(),
            IsOfferedQ1: fall,
            IsOfferedQ2: winter,
            IsOfferedQ3: spring,
            IsOfferedQ4: summer,
            Prerequisite1: $('#pythonPrerequisite1').val(),
            Prerequisite2: $('#pythonPrerequisite2').val(),
            Prerequisite3: $('#pythonPrerequisite3').val(),
            IsTentative: tenativeCourse,
            Prerequisites: $('#pythonPrerequisites').val()
        };
        courseDataToPost = JSON.stringify(Course);
        return courseDataToPost;

Next, I will pass this JSON String to my AJAX CALL like so –

$.ajax({
    type: "POST",
    url: "/api/courses",
    cache: false,
    contentType: "application/json; charset=utf-8",
    async: true,
    data: dataToPost,
    dataType: "json",
    success: function () {
      alert("Success!");
    },
    error: function (x, e) {
      alert("The call to the server side FAILED. " + x.responseText);
    }
});

So, this successfully routes the request to the correct controller’s method here –

    // POST /api/courses
    [HttpPost]
    public void Post(pythonCourse course)
    {
        var postTableAdapter = new tnpCourseTableAdapter();
        decimal zero = 0;
        string no = "N";

        try
        {
            postTableAdapter.Insert(course.CourseID, course.CourseLongName, 
                                    course.IsActiveCourse, course.Description,
                                    course.DepartmentID, course.LabHours, 
                                    course.LectureHours, course.CourseShortName,
                                    course.EffectiveYear, course.EffectiveQuarter, 
                                    course.IsClearanceRequired,
                                    course.ClearanceRequired, 
                                    course.CourseCoordinatorID, course.IsOfferedQ1,
                                    course.IsOfferedQ2, course.IsOfferedQ3, 
                                    course.IsOfferedQ4, course.Prerequisite1,
                                    course.Prerequisite2, course.Prerequisite3, 
                                    course.IsTentative, zero, zero, null,
                                    no, no, no, null, null, null, null);
        }
        catch (Exception)
        {
            throw;
        }            
    }            

When I arrive here in the server code, the debugger stops on the catch block and gives the “String or binary data…” error.

Notice in my list of arguments that I pass 3 instances of the string no.

These arguments are strings, but SQL Server is expecting VARCHAR(1).

Is this what is causing my grief, or is there something else going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:06:25+00:00Added an answer on June 18, 2026 at 11:06 am

    This error means you’re putting too much data into the column. Your column expects a single character string.

    So the error means if you put no into the column.. it will be truncated to n.

    Another example: If the column was VARCHAR(5) and you inserted “Hello World!”, it would warn that it can only insert “Hello”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an web app that uses this code to search for addresses: http://code.google.com/intl/en/apis/maps/documentation/javascript/examples/places-autocomplete.html
I'm building a Web API that will require a developer or API key for
I'm looking at building a simple web app that will expose an API that
I am building a Web API using MVC4 and some request return blocks of
I've been researching on building web applications in a true client-server fashion. This type
I see in this question that WCF Web API is still in preview and
I'm currently building a Web API and have a specific scenario that I cannot
I'm building a new web application that will hopefully be used to serve geo
I need to integrate a third-party video API solution into my web application that
I'm building a web app which will have an api and an authorization service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.