Inserting row from control to table,then some time value getting null

Analyst_SQL 3,531 Reputation points
2023-03-20T16:15:14.7533333+00:00

I am inserting rows into table ,some time inserting null values from dropdownlist to table below is image.

Code

    $(document).ready(function () {
                    $("#Search_Prdno").change(function () {


                        var prdno = $("#Search_Prdno").val();
                        if (prdno != "") {
                            GetBarcodeSB(prdno);
                            GetBarcodeweight(prdno);
                          
                        }
                        else {

                            $("#codeitem").empty();
                        }

                    });
                });

                function GetBarcodeSB(prdno) {

                    $.ajax({
                        async: true,
                        type: 'GET',
                        dataType: 'JSON',
                        contentType: 'application/json; charset=utf-8',
                        url: '/OrderPack/GetBarcodeSB',
                        data: { prdno: prdno },
                        success: function (data) {
                            $('#select2-3').empty();
                            $(data).each(function (index, item) {
                                $('#select2-3').append($('<option/>', { value: item.Value, text: item.Text }))
                                    ;

                            })
                        },
                        error: function () {
                            alert("There is some problem to get.");
                            $("#P_Prdno").empty();
                            $("#P_OrderNo").empty();
                            $("#I_Weigth").empty();
                        }
                    });
                }


                function GetBarcodeweight(prdno) {

                    $.ajax({
                        async: true,
                        type: 'GET',
                        dataType: 'JSON',
                        url: '/OrderPack/GetBarcodeweight',
                        data: { prdno: prdno },
                        success: function (data) {
                            console.log(data);
                            //populate the didden field
                            $("#Prdno").val(data.Prdno);
                            //populate the label
                            $("#Barcode_Bale").val(data.Prdno);

                            //populate the didden field
                            $("#OrderNo").val(data.OrderNo);
                            //populate the Orderno
                            $("#P_OrderNo").text(data.OrderNo);

                            //populate the didden field
                            $("#weight").val(data.weight);
                            //populate the label
                            $("#I_Weigth").text(data.weight);

                            addrow(prdno);
                            ////console.log($("#select2-1").val());
                            //var orderNo1 = $('#P_OrderNo').html();
                            //var orderNo2 = $('#Orderno-label-5').html();
                            //if (orderNo1 == orderNo2) {
                            //    alert('same order');
                            //}
                            //else {
                            //    alert('Not same order');

                            //}

                        },
                        //error: function () {
                        //    //alert("There is some problem to get.");

                        //}
                    });
                }



                function addrow() {
                    $('#btnInsert').add(function () {
                        var tbody = $('#DataInsert tbody');
                        var tr = $('<tr></tr>');
                        tr.append('<td>' + $('#Barcode_Bale').val() + '</td>');
                        tr.append('<td>' + $('#Bale_Qty').val() + '</td>');
                        tr.append('<td>' + $('#select2-3').text() + '</td>');
                        tr.append('<td>' + $('#select2-3').val() + '</td>');

                        tr.append('<td><input class="del" type="button" value="Delete" /></td>')
                        tbody.append(tr);


                        $("#Barcode_Bale").empty();
                        $("#select2-3").empty();
                     
                    });

                    $('#btnSave').click(function () {
                        var smallbales = new Array();
                        $("#DataInsert tbody tr").each(function () {
                            var row = $(this);
                            var smallbale = {};
                            smallbale.Barcode_Bale = row.find("TD").eq(0).html();
                            smallbale.Bale_Qty = row.find("TD").eq(1).html();
                            smallbale.select2 = row.find("TD").eq(2).html();
                            smallbale.select2 = row.find("TD").eq(3).html();

                            smallbales.push(smallbale);
                        });
                        $.ajax({
                            type: "POST",
                            url: '/OrderPack/InsertPack',
                            data: JSON.stringify(smallbales),
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (r) {
                                alert("Record(s) inserted.");
                            },
                            error: function (r) {
                                alert(r.responseText);
                            }
                        });

                    });
                };

    <div style="display: inline-block;">
                    <div>
                        @Html.LabelFor(model => model.Codeitem, "Select Item", htmlAttributes: new { @class = "control-label col-md-3" })

                        @Html.DropDownList("codeitemId", (SelectList)ViewBag.Codeitem, "Select", htmlAttributes: new { @class = "form-control", @id = "select2-3" })

                        @Html.ValidationMessageFor(model => model.Codeitem, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div style="display: inline-block;">
                    <div>
                        @Html.LabelFor(model => model.Bale_QTY, "Bale_QTY", htmlAttributes: new { @class = "control-label col-md-4" })

                        @Html.EditorFor(model => model.Bale_QTY, new { htmlAttributes = new { @class = "form-control col-md-10", @id = "Bale_Qty", @Value = "1" } })
                        @Html.ValidationMessageFor(model => model.Bale_QTY, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div style="display: inline-block;">
                    <div>
                        @Html.LabelFor(model => model.Barcode_Bale, "BarCode", htmlAttributes: new { @class = "control-label col-md-4" })

                        @Html.EditorFor(model => model.Barcode_Bale, new { htmlAttributes = new { @class = "form-control col-md-10", @id = "Barcode_Bale" } })
                        @Html.ValidationMessageFor(model => model.Barcode_Bale, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div style="display: inline-block;">

                    @Html.LabelFor(model => model.OrderNo, "OrderNo", htmlAttributes: new { @class = "control-label col-md-2", @id = "P_OrderNo" })
                    @Html.HiddenFor(Model => Model.OrderNo)

                </div><div style="display: inline-block;">

                    @Html.LabelFor(model => model.weight, "Weight", htmlAttributes: new { @class = "control-label col-md-2", @id = "I_Weigth" })
                    @Html.HiddenFor(Model => Model.weight)

                </div>
            </div>
            <div>
                <input type="button" value="Insert" id="btnInsert" />
                <br />
                <br />
                <table id="DataInsert">
                    <thead>
                        <tr>
                            <th>Barcode</th>
                            <th>QTY</th>

                            <th>Item</th>
                            <th>CodeiTem</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
                <br />
                <input type="button" value="Save" id="btnSave" />
            </div>
        </div>
    

User's image

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,251 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,191 Reputation points
    2023-03-21T06:02:10.7233333+00:00

    Clearly, your code does not check for a null and there are no database constraints to stop from inserting a null field. The first step is fixing your code so a null is not allowed. The next step is basic troubleshooting to figure out where the null is coming form.

    Unfortunately, the code you've shared does not show any logic that saves the data. If you expect the community to troubleshoot your code then it is your responsibility to provide code the community can actually run with the steps to reproduce the unwanted behavior. Otherwise, we can only guess what is wrong with your code.

    Keep in mind, that you are in the best position to debug your code since you can run the code, have the data, can reproduce the problem, and have access to standard debugging tooling.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful