【问题标题】:i have a autocomplete textbox and when i press the button then insert into database我有一个自动完成文本框,当我按下按钮然后插入数据库
【发布时间】:2017-10-24 10:46:58
【问题描述】:

错误

INSERT 语句与 FOREIGN KEY 约束“FK_CustomizedReportProducts_CustomizedReportSections”冲突。冲突发生在数据库“LarvolDelta”、表“dbo.CustomizedReportSections”、列“customized_report_section_id”中。

代码

$('#btnProducts').click(function () {
            debugger;  
            var itemId = $(".prodId").val();
            var productid = $(".prodId").val();
            if (itemId == '') return;
            var itemValue = $(".prodName").val();
            var treeHtml = "";
            var sectionid = '00000000-0000-0000-0000-000000000000';

            var commentRegion = "";
            var sectionText = $("#txtProducts").val();
            if (sectionText == "")
                return;
            var itemFound = false;
            var ProductRatingRegion = "";
            ProductRatingRegion += "<div id='dialogPIR" + itemId + "' class='dialogPIR' title='<%=lblPIRCaption%>" + " for " + itemValue + "' style='display:none'>";
            ProductRatingRegion += "<span id='spanPIR" + itemId + "'></span>";
            ProductRatingRegion += "<div style='text-align:left;margin-top: 20px;'>";
            ProductRatingRegion += "<div style='z-index:1;' id='PIRSlider' name='PIRSlider' class='PIRSlider" + itemId + "'></div><br/>";
            ProductRatingRegion += "<div class='steps' style='width: 216px; height: 40px; margin-left:-1px; margin-top: -25px; color: gray; z-index:0;'>";
            ProductRatingRegion += "<span style='width:6px; left:0%;'>|<br />NA</span>";
            ProductRatingRegion += "<span style='left:10%;'>|<br />1</span>";
            ProductRatingRegion += "<span style='left:20%;'>|<br />2</span>";
            ProductRatingRegion += "<span style='left:30%;'>|<br />3</span>";
            ProductRatingRegion += "<span style='left:40%;'>|<br />4</span>";
            ProductRatingRegion += "<span style='left:50%;'>|<br />5</span>";
            ProductRatingRegion += "<span style='left:60%;'>|<br />6</span>";
            ProductRatingRegion += "<span style='left:70%;'>|<br />7</span>";
            ProductRatingRegion += "<span style='left:80%;'>|<br />8</span>";
            ProductRatingRegion += "<span style='left:90%;'>|<br />9</span>";
            ProductRatingRegion += "<span style='left:100%;'>|<br />10</span>";
            ProductRatingRegion += "</div>";

            ProductRatingRegion += "<input type='button' value='<%=lblPIRButtonText%>' onclick='PIRAddedHandler(\"" + itemId + "\")'/>";
            ProductRatingRegion += "</div>";
            ProductRatingRegion += "</div>";

            commentRegion += "<div id='dialog" + itemId + "' class='dialog' title='Middle Column for " + itemValue + "' style='display:none'>";
            commentRegion += "<span id='spanComments" + itemId + "'></span>";
            commentRegion += "<p style='text-align:left;margin-top: 20px;'>";
            commentRegion += "<textarea id='newComment' name='comments' class='commentInputBox" + itemId + "' cols='55' rows='3'>Enter your comments here...</textarea><br/>";
            commentRegion += "<input type='button' value='Add Middle Column' onclick='commentAddedHandler(\"" + itemId + "\")'/>";
            commentRegion += "</p>";
            commentRegion += "</div>";

            treeHtml += '<li><img src="../../Images/Icons/Cross.png" style="cursor:pointer; float:left; width:14px; margin-right:4px;" onclick="DeleteCurrentLi(this)";/><span class="file product_element" id="' + itemId + '">&nbsp;&nbsp;' + sectionText + '</span>' + "  "
            treeHtml += '<img src="../../Images/Icons/comments.png" title="Middle Column" class="commentsIcon" onclick="commentIconClickHandler(\'' + itemId + '\')"/>' + " "
            treeHtml += '<img src="../../Images/Icons/PIRIcon.png" title="<%=lblPIRCaption%>" class="PIRIcon" product_id="' + itemId + '" id="pir_icon' + itemId + '" />'
                    + '<ul class="selectable">';
            treeHtml += '</ul></li>';

            $("ul#productTreeview").sortable({
                revert: false
            });

            // $("ul, li").disableSelection();

            $("ul#productTreeview li").draggable({
                appendTo: "body",
                helper: "clone",
            });

            var key, value;
            if (li.length === 0) {

                $("ul#productTreeview").append(treeHtml + commentRegion + ProductRatingRegion);
                var itemId = sectionText;
                li.push(itemId);

            }
            else {
                for (key in li) {
                    if (li.hasOwnProperty(key) && !isNaN(parseInt(key, 10))) {
                        value = li[key];
                        if (value.substring() === sectionText)
                            itemFound = true;
                    }

                }
                if (itemFound == true) {
                    // alert('item already present');
                    ShowPopup($('#dialog').dialog('open'));

                }
                else {

                    $("ul#productTreeview").append(treeHtml + commentRegion + ProductRatingRegion);
                    var itemId = sectionText;
                    li.push(itemId);
                }
            }

            $("#txtProducts").val("");
            SaveCustomizedRecord(productid, reportId, sectionid);
        });

    });

【问题讨论】:

  • 猜测:SaveCustomizedRecord() 以某种方式创建了外键约束错误。您似乎缺少一些报告左右 - 检查您的原始数据并与您输入到 SaveCustomizedRecord 的原始数据进行比较 - 特别是检查表中是否存在 ID。没有您的保存方法和您的表格内容,这有点像看着水晶球并猜测。
  • 我想在按钮点击时调用一个 webmethod,我该怎么做呢

标签: jquery .net database webmethod


【解决方案1】:

问题是 sectionid 试图插入数据库中不存在的值

您可能需要设置正确的值

var sectionid = '00000000-0000-0000-0000-000000000000';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2013-04-15
    • 1970-01-01
    相关资源
    最近更新 更多