Insert SQL query with AJAX and ASP.Net

0

Hello, I need to do a SQL INSERT using ajax in ASP.NET, I'm not sure what I'm missing.

$('#submit').click(function () {
    insertFeedback();
});

function insertFeedback()
{
    var modelx = new Object();

    var depS = document.getElementById("resultDep");
    var catS = document.getElementById("resultCat");
    var schemS = document.getElementById("schemes");
    var wiw = document.getElementById("wiw").value;

    modelx.wiw = wiw;

    var dp  = depS.options[depS.selectedIndex].value;
    modelx.dep = dp;

    var ct = catS.options[catS.selectedIndex].value;
    modelx.cat = ct;
   
    var sch  = schemS.options[schemS.selectedIndex].value;
    modelx.schem = sch;

    var randid = Math.floor((Math.random() * 100000) + 1);
    modelx.id = randid;

   // modelx.file = $('#formato').val();

    alert(modelx.wiw + "/" + modelx.dep + "/" + modelx.cat + "/" + modelx.schem +
        "/ route: /" + "/" + modelx.id);

    $.ajax({
        type: "POST",
        url: 'Url.Action("CreateRequest", "Home")',
        data: modelx,
        dataType: "json",
        success: function (data) {
            alert("hi");
            // your code
            $('#sucess').removeClass('hidden');

        },
        error: function (result) {
            $('#error').removeClass('hidden');
        }
    })
   }
 


So far I have no problem in pulling all the data I need from the user. My problem I imagine has to be around here on the server side. But the conosla does not print any error.

  [WebMethod]
    public JsonResult CreateRequest(Requests modelx)
    {
        string cs = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
        string sql =
            "INSERT INTO [DB_PCC].[dbo].[Requests]"+
            "([Id],[Wiw],[UserId],[Project],[SchemeId],[Status],"+
            "[ExpirationDays],[Comments],[DepartmentId]"+
            ",[CategoryId],[seen],[Light],[Edit],[CreateDate],"+
            "[SeenDate],[SendDate],[AnswerDate]"+
            ",[EditDate],[Sent],[Approved],[Rejected])"+
            "VALUES"+
            "(@id,'@wiw',333,'test me and thn',@scheme,'En espera',3,'none'" +
            ",@department ,@category,0,1,0,'none' ,'none'" +
            ",'none' ,'none','none',0,0,0)"       
        ;

      
        using (SqlConnection con = new SqlConnection(cs))
        {
            using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                cmd.Parameters.AddWithValue("@id", modelx.Id);
                cmd.Parameters.AddWithValue("@wiw", modelx.Wiw);
                cmd.Parameters.AddWithValue("@departmen", modelx.DepartmentId);
                cmd.Parameters.AddWithValue("@category", modelx.CategoryId);
                cmd.Parameters.AddWithValue("@scheme", modelx.SchemeId);

                con.Open();
                int result = cmd.ExecuteNonQuery();

                // Check Error
                if (result < 0) {
                    Console.WriteLine("Error inserting data into Database!");
                }
                    
            } 

           
                return Json(new { success = true, message = "all good fella" }, JsonRequestBehavior.AllowGet);
          

        }
        // End GetCategories
    }
 Many thanks to the whole community. They are great :)     
asked by E.Rawrdríguez.Ophanim 18.10.2017 в 23:38
source

1 answer

1

I'm not sure if it can work, it's still complicated for me to give answers without having the code in front of me running correctly but use this data: JSON.PARSE (modelx) within the AJAX call

    
answered by 19.10.2017 / 18:18
source