locked
value saved is always 0 (zero) in MySqlCommand Insert statement! RRS feed

  • Question

  • User991566988 posted

    Hi All,

    I've wen api application, this is my model:

    public class past_med_hist
    
    {
    
        public int weight;
    
    }

    and this is my web api HealthController:

        public class HealthController : ApiController
        {     
            string strConnection = ConfigurationManager.ConnectionStrings["test"].ConnectionString.ToString();
            public void Create(past_med_hist health)
            {
                using (MySqlConnection con = new MySqlConnection(strConnection))
                {
                    MySqlCommand cmd = new MySqlCommand("insert into past_med_hist(uid, rec_date, weight) values('" + 5 + "','" + "2017-07-14" + "','" + health.weight + "')", con);
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }
    
    

    and my view:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Create</title>
        <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    
    </head>
    <body>
    
        <h2>Create Form</h2>
    
        <form>
    
            <div class="container">          
                <input type="text" id="weight" name="weight" required>         
            </div>
            <div class="container">
                <input type="submit" class="btn btn-success" value="Create" />
                <input type="button" class="btn btn-primary" onclick="location.href='index.html';" value="Login" />
    
            </div>
    
    
        </form>
    
    </body>
    </html>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
    
        $("form").submit(function (event) {
            event.preventDefault();
            var weightv = $("#weight").val();
            if ($.trim(weightv).length > 0) {
                $.ajax({
                    type: "post",
                    dataType: 'json',
                    url: 'http://localhost:10112/api/Health/Create',
                    data: { weight: weightv },
                    crossDomain: true,
                    cache: false,
                    success: function (data) {
                        alert('success');
    
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        // If error happened
                    }
                });
            } else {
                alert('Please fill the required fields!');
            }
        });
    </script>
    
    

    the insert statement is success but the value for weight column is always 0!!! why? and how to solve plz?

    Saturday, July 15, 2017 8:41 AM

Answers

All replies