Note that there are some explanatory texts on larger screens.

plurals
  1. POVARCHAR(1) equivalent in C# land, SQL INSERT ERROR
    primarykey
    data
    text
    <p>I am building a Web API that will be inserting some values into a Microsoft SQL Server database.</p> <p>This APIs eventual usage will be in a JAVA Spring application.</p> <p>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.</p> <p>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 -</p> <blockquote> <p><em>An error has occurred.","ExceptionMessage":"String or binary data would be truncated.</em></p> </blockquote> <p>And the stack trace follows. </p> <blockquote> <p>at System.Data.SqlClient.SqlInternalConnection.OnError... blah blah</p> </blockquote> <p>I think the problem is with several of the columns that I am attempting to insert into.</p> <p>They are of type <code>VARCHAR(1)</code> and have the <code>NOT NULL</code> property in SQL Server.</p> <p>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 <code>INSERT</code> on the server. Incidentally, I am using the <code>TableAdapter</code> to handle all my database connections, etc...</p> <p>Code that makes all this happen is as follows:</p> <pre><code>//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; </code></pre> <p>Next, I will pass this JSON String to my AJAX CALL like so -</p> <pre><code>$.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); } }); </code></pre> <p>So, this successfully routes the request to the correct controller's method here -</p> <pre><code> // 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; } } </code></pre> <p>When I arrive here in the server code, the debugger stops on the catch block and gives the "String or binary data..." error.</p> <p>Notice in my list of arguments that I pass 3 instances of the string <code>no</code>.</p> <p>These arguments are strings, but SQL Server is expecting <code>VARCHAR(1)</code>.</p> <p>Is this what is causing my grief, or is there something else going on?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload