Click here to Skip to main content
15,069,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,, I'm get error when save data to database using ajax. When the store prosedure use this working, but just save null value on database. How to solve this, any help could be apriciate.

This is the screen shoot :

1. https://pasteboard.co/Kf41y8G.jpg

2. https://pasteboard.co/Kf421zJ.jpg

3. https://pasteboard.co/Kf42hjui.jpg

What I have tried:

SQL
ALTER PROCEDURE [dbo].[spAddDetailPinjamanBuku] 
	-- Add the parameters for the stored procedure here
	@Kode_Pinjam varchar(50) =null, 
	@Kode_Buku varchar(50) =null, 
	@Judul varchar(MAX) =null, 
        @Penulis varchar(MAX) =null, 
	@Penerbit varchar(MAX) =null
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	Insert Into Detail_Pinjaman 
	(Kode_Pinjam, Kode_Buku, Judul, Penulis, Penerbit) 
	Values 
	(@Kode_Pinjam, @Kode_Buku, @Judul, @Penulis, @Penerbit)
END

if use this store prosedure get error "System.Data.SqlClient.SqlException (0x80131904): Procedure or function 'spAddDetailPinjamanBuku' expects parameter '@Kode_Pinjam', which was not supplied."

SQL
ALTER PROCEDURE [dbo].[spAddDetailPinjamanBuku] 
	-- Add the parameters for the stored procedure here
	@Kode_Pinjam varchar(50), 
	@Kode_Buku varchar(50), 
	@Judul varchar(MAX), 
	@Penulis varchar(MAX), 
	@Penerbit varchar(MAX)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	Insert Into Detail_Pinjaman 
	(Kode_Pinjam, Kode_Buku, Judul, Penulis, Penerbit) 
	Values 
	(@Kode_Pinjam, @Kode_Buku, @Judul, @Penulis, @Penerbit)
END


This the Controller.cs
C#
//Post method to Save DetailPinjamanBuku
        [HttpPost]
        public JsonResult SaveData_Search_Buku(PinjamanModel pinjamanBuku)
        {
            AddSearch_Buku(pinjamanBuku);
            return Json(pinjamanBuku);
        }

        //Method for Adding DetailPinjamanBuku
// ***** this is where you need to capture the details *****
        private void AddSearch_Buku(PinjamanModel pinjamanBuku)
        {
            using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("db_perpustakaan")))
            {
                using (SqlCommand cmd = new SqlCommand("spAddDetailPinjamanBuku"))
                {
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;


                    cmd.Parameters.AddWithValue("@Kode_Pinjam", pinjamanBuku.Kode_Pinjam);
                    cmd.Parameters.AddWithValue("@Kode_Buku", pinjamanBuku.Kode_Buku);
                    cmd.Parameters.AddWithValue("@Judul", pinjamanBuku.Judul);
                    cmd.Parameters.AddWithValue("@Penulis", pinjamanBuku.Penulis);
                    cmd.Parameters.AddWithValue("@Penerbit", pinjamanBuku.Penerbit);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    cmd.Connection.Close();
                }
            }
        }
​


Create.cshtml
HTML
@using www.si.perpustakaan.Models
@model PinjamanModel
@{
    ViewData["Title"] = "Create";
    Layout = "~/Views/Shared/AdminDashboard/_Layout.cshtml";
}

<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script type="text/javascript" src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/assets/plugins/jquery-ui/jquery-ui.js"></script>
<link href="~/assets/plugins/jquery-ui/jquery-ui.css" rel="stylesheet" />

<div class="tab-content">
    <div class="tab-pane tabs-animation fade show active" id="tab-content-0" role="tabpanel">
        <div class="row">
            <div class="col-md-12">
                <div class="main-card mb-3 card">
                    <div class="card-body">
                        <!-- Modal Header -->
                        <div class="modal-header bg bg-success">
                            <div align="center">
                                <h4 class="modal-title">Add Data Pinjaman</h4>
                            </div>
                        </div>
                        <hr />
                        <form asp-action="" asp-controller="" method="post">
                            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="position-relative form-group">
                                        <label asp-for="Kode_Pinjam" class="control-label"></label>
                                        <input asp-for="Kode_Pinjam" id="txtKode_Pinjam" class="form-control" readonly="readonly" />
                                        <span asp-validation-for="Kode_Pinjam" class="text-danger"></span>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="position-relative form-group">
                                        <label asp-for="Tanggal_Pinjam" class="control-label"></label>
                                        <input asp-for="Tanggal_Pinjam" class="form-control" placeholder="Enter Tanggal Pinjam Buku" />
                                        <span asp-validation-for="Tanggal_Pinjam" class="text-danger"></span>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="position-relative form-group">
                                        <label asp-for="Tanggal_Kembali" class="control-label"></label>
                                        <input asp-for="Tanggal_Kembali" class="form-control" placeholder="Enter Tanggal Kembali Buku" />
                                        <span asp-validation-for="Tanggal_Kembali" class="text-danger"></span>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="position-relative form-group">
                                        <label asp-for="Kode_Anggota" class="control-label"></label>
                                        <div class="input-group">
                                            <input asp-for="Kode_Anggota" class="form-control" id="txtKodeAnggota" placeholder="Enter Kode Anggota" required />
                                            <div class="input-group-append">
                                                <button type="button" id="CekNamaAnggota" value="Cari Kode Anggota" class="btn btn-warning" />
                                            </div>
                                        </div>
                                        <span asp-validation-for="Kode_Anggota" class="text-danger"></span>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="position-relative form-group">
                                        <label asp-for="Nama_Anggota" class="control-label"></label>
                                        <input asp-for="Nama_Anggota" class="form-control" id="txtNamaAnggota" placeholder="Enter Nama Anggota" readonly="readonly" />
                                    </div>
                                    <span asp-validation-for="Nama_Anggota" class="text-danger"></span>
                                </div>

                                <div class="col-md-6">
                                    <div class="position-relative form-group">
                                        <label asp-for="Kode_Petugas" class="control-label"></label>
                                        <div class="input-group">
                                            <input asp-for="Kode_Petugas" class="form-control" id="txtKodePetugas" placeholder="Enter Kode Petugas" required />
                                            <div class="input-group-append">
                                                <button type="button" id="CekNamaPetugas" value="Cari Kode Petugas" class="btn btn-warning" />
                                            </div>
                                        </div>
                                        <span asp-validation-for="Kode_Petugas" class="text-danger"></span>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="position-relative form-group">
                                        <label asp-for="Nama_Petugas" class="control-label"></label>
                                        <input asp-for="Nama_Petugas" class="form-control" id="txtNamaPetugas" placeholder="Enter Nama Petugas" readonly="readonly" />
                                    </div>
                                    <span asp-validation-for="Nama_Petugas" class="text-danger"></span>
                                </div>

                                @*Data Buku*@
                                <div class="col-md-12">
                                    <div class="main-card mb-3 card">
                                        <div class="card-body">
                                            <!-- Modal Header -->
                                            <div class="modal-header bg bg-primary">
                                                <div align="center">
                                                    <h4 class="modal-title">Data Buku</h4>
                                                </div>
                                            </div>
                                            <hr />

                                            <table>
                                                <thead>
                                                    <tr>
                                                        <th>Kode Buku</th>
                                                        <th>Judul</th>
                                                        <th>Penulis</th>
                                                        <th>Penerbit</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td><input asp-for="Kode_Buku" class="form-control" placeholder="Enter Kode Buku" id="txt_Search_Kode_Buku" name="Kode_Buku" required /></td>
                                                        <td><input asp-for="Judul" class="form-control" placeholder="Enter Judul Buku" id="txtJudul" name="Judul" readonly="readonly" /></td>
                                                        <td><input asp-for="Penulis" class="form-control" placeholder="Enter Penulis" id="txtPenulis" name="Penulis" readonly="readonly" /></td>
                                                        <td><input asp-for="Penerbit" class="form-control" placeholder="Enter Penerbit" id="txtPenerbit" name="Penerbit" readonly="readonly" /></td>
                                                        <td><button type="button" id="Btn_Search_Kode_Buku" class=" btn btn-warning btn-sm"></td>
                                                        <td><button type="button" onclick="return Btn_Add_Search_Kode_Buku(this);" id="Add_Search_Kode_Buku" class=" btn btn-success btn-sm">^__i class="fa fa-plus"> Add</button></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-12">
                                    <div class="form-group">
                                        <div align="center">
                                            <input type="submit" value="Save Data Pinjaman" class="btn btn-success" />
                                        </div>
                                    </div>
                                    <div>
                                        <a asp-action="Index">^__i class="fa fa-arrow-alt-circle-left"> Back to List</a>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" lang="javascript">
    $(document).ready(function(){
        Btn_Add_Search_Kode_Buku();
    });
    //$(document).on('submit', function (event) {
    //    event.preventDefault();
        // everything else you want to do on submit
    function Btn_Add_Search_Kode_Buku() {
        var _ObjectDetailPinjamanBuku = {
            Kode_Pinjam: $('#txtKode_Pinjam').val(),
            Kode_Buku: $('#txt_Search_Kode_Buku').val(),
            Judul: $('#txtJudul').val(),
            Penulis: $('#txtPenulis').val(),
            Penerbit: $('#txtPenerbit').val()
        }
        $.ajax({
            url: "@Url.Action("SaveData_Search_Buku", "Pinjaman")",
            data: JSON.stringify(_ObjectDetailPinjamanBuku),
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function () {
                window.alert('Data berhasil disimpan...');
            }
        });
    }
</script>

<script type="text/javascript" lang="javascript">
    $(document).ready(function () {
        $('#Btn_Search_Kode_Buku').on("click", function () {
             $.ajax({
                url: "@Url.Action("Get_DataBukuByID", "Pinjaman")",
                type: "GET",
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                data: { Kode_Buku: $('#txt_Search_Kode_Buku').val() },
                 success: function (data) {
                     if (data !="") {
                         $('#txtJudul').val(data.Judul);
                         $('#txtPenulis').val(data.Penulis);
                         $('#txtPenerbit').val(data.Penerbit);
                     }
                     else {
                         window.alert(' error : ' + data.message);
                     }

                 }
             });
        });
        //return false;
    });
</script>


<script type="text/javascript" lang="javascript">
    $(document).ready(function () {
        $("#CekNamaAnggota").click(function () {
            $.ajax({
                url: "@Url.Action("SearchNamaAnggota", "Pinjaman")",
                type:"POST",
                data: { Kode_Anggota: $("#txtKodeAnggota").val() },
                success: function (data) {
                    if (data != "") {
                        $("#txtNamaAnggota").val(data);
                    }
                    else {
                        alert("Kode Anggota tidak ditemukan !!!");
                        $("#txtKodeAnggota").val("");
                        $("#txtNamaAnggota").val("");
                    }
                }
            });
        });
    });
</script>

<script type="text/javascript" lang="javascript">
    $(document).ready(function () {
        $("#CekNamaPetugas").click(function () {
            $.ajax({
                url: "@Url.Action("SearchNamaPetugas", "Pinjaman")",
                type:"POST",
                data: { Kode_Petugas: $("#txtKodePetugas").val() },
                success: function (data) {
                    if (data != "")
                    {
                        $("#txtNamaPetugas").val(data);
                    }
                    else
                    {
                        alert("Kode Petugas tidak ditemukan !!!");
                        $("#txtKodePetugas").val("");
                        $("#txtNamaPetugas").val("");
                    }
                }
            });
        });
    });
</script>

<script type="text/javascript" lang="javascript">
    $("[id*=Tanggal_Pinjam]").attr("ReadOnly", true)
    $("[id*=Tanggal_Pinjam]").datepicker(
        {
            dateFormat: "yy/mm/dd"
        });

    $("[id*=Tanggal_Kembali]").attr("ReadOnly", true)
    $("[id*=Tanggal_Kembali]").datepicker(
        {
            dateFormat: "yy/mm/dd"
        });
</script>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
​
Posted
Updated 12-Aug-21 2:52am
v2
Comments
Richard MacCutchan 9-Aug-21 5:40am
   
The message is quite clear. somewhere in your code you are calling the stored procedure without the mentioned parameter.
tri setia 9-Aug-21 6:39am
   
yes l know sir..
l was tried to solve that but l can't find the solution.
please help correct my code above sir.
Richard MacCutchan 9-Aug-21 6:57am
   
How can I correct your code? I have no idea where this error occurs, or what parameters it should be using.

Use the debugger to find out why you are not passing the required parameter.
tri setia 9-Aug-21 8:31am
   
I was change like this sir, but still get same error.
Data passing correctly on web debugger as Json like this the screenshot sir https://pasteboard.co/Kf5oFGM.jpg
Richard MacCutchan 9-Aug-21 9:05am
   
But that does not help. You need to capture the point where you send the data to the stored procedure and check all the parameters.
Richard MacCutchan 11-Aug-21 8:20am
   
See my comment in Controller.cs, headed "// *****". That is where you need to debug from to see exactly what data you are passing to the stored procedure.
tri setia 11-Aug-21 11:37am
   
I was change the controller.cs headed to insert the data without Ajax to know controller.cs headed it work or not in fact all data inserted properly to database, I think the controller.cs headed not the problem, because when I use Ajax to insert data the problem occurs.

We can't fix your code. You have to develop some form of debugging ability.

Use the developer console in the browser to find out what's happening. You'll probably have to add console.log() statements to your javascript at key points to help you determine where you're going wrong.
   
Comments
tri setia 11-Aug-21 6:14am
   
thanks for the advice sir..
I'm still tried to find the solution now..
Problem Solved
The Ajax
<script type="text/javascript">
    $(document).ready(function () {
        $('#Btn_Add_Search_Kode_Buku').on("click", function () {
            var ObjectBuku = {
                Kode_Pinjam: $("#Kode_Pinjam").val(),
                Kode_Buku: $("#Kode_Buku").val(),
                Judul: $("#Judul").val(),
                Penulis: $("#Penulis").val(),
                Penerbit: $("#Penerbit").val()
            };
            $.ajax({
                url: "/Pinjaman/SaveData_Search_Buku",
                data: JSON.stringify(ObjectBuku),
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    window.alert("Data berhasil disimpan..");
                },
                error: function (errormessage) {
                    window.alert(errormessage.responseText);
                }
            });
            return false;
        });
    });
</script>


Controller.cs
[HttpPost]
        public JsonResult SaveData_Search_Buku([FromBody]Detail_Pinjaman_Buku pinjaman_Buku)
        {
            int i = 0;
            using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("db_perpustakaan")))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "Insert Into Detail_Pinjaman (Kode_Pinjam, Kode_Buku, Judul, Penulis, Penerbit) Values (@Kode_Pinjam, @Kode_Buku, @Judul, @Penulis, @Penerbit)";

                    cmd.Parameters.AddWithValue("@Kode_Pinjam", pinjaman_Buku.Kode_Pinjam);
                    cmd.Parameters.AddWithValue("@Kode_Buku", pinjaman_Buku.Kode_Buku);
                    cmd.Parameters.AddWithValue("@Judul", pinjaman_Buku.Judul);
                    cmd.Parameters.AddWithValue("@Penulis", pinjaman_Buku.Penulis);
                    cmd.Parameters.AddWithValue("@Penerbit", pinjaman_Buku.Penerbit);

                    i = cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            return Json(i);
        }
   

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