Insert different Modal popup data to different table using jQuery Ajax in ASP.Net

Last Reply on Sep 22, 2017 05:50 AM By AnandM

Posted on Sep 22, 2017 04:35 AM

I have two models in a page which is used to insert value to the database. If I use first model to insert value the insertion becomes success but it effect two tables in db. When I use 2nd model to insert the value the insertion becomes success and it only effect only one table. The error occurs only when I try to insert using 1st model.

Also when inserting using 2nd model the inserted id also effecting slno column which is set as identity increment as true and still it effect that column

here is error screenshot link

https://imgur.com/a/8PWjz

 

    <script type="text/javascript">
        $(function () {
            $('[id*=btnSave]').click(function () {
                var UserDetail = {};
                debugger;
                UserDetail.unitid = $('[id*=txtPassword]').val();
                UserDetail.unittype = $('[id*=txt_qty]').val();
               
                $.ajax({
                    type: "POST",
                    url: "product.aspx/UserDetails",
                    data: '{UserDetail :' + JSON.stringify(UserDetail) + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert(response.d);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });
    </script>
    <script type="text/javascript">
        $(function () {
            $('[id*=btnSave1]').click(function () {
                var UserDetail1 = {};
                debugger;
                UserDetail1.catid = $('[id*=txt_new_catid]').val();
                UserDetail1.mprodcat = $('[id*=txt_newcateogry]').val();
               
                $.ajax({
                    type: "POST",
                    url: "product.aspx/UserDetails1",
                    data: '{UserDetail1 :' + JSON.stringify(UserDetail1) + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert(response.d);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });
    </script>

 

<div>
    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">
        Add New Category</button>
    <div id="myModal1" class="modal fade" role="dialog">
        <div class="modal-dialog" style="width: 400px;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        &times;</button>
                    <h4 class="modal-title">
                        Add New Category</h4>
                </div>
                <div class="modal-body">
                    <table>
                       
                        <tr>
                            <td>
                                <span class="text-danger">Cate Id: </span>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_new_catid" CssClass="form-control" Width="250px" runat="server" ReadOnly="true" />
                            </td>
                        </tr>
                         <tr>
                            <td>
                                <span class="text-danger">Category: </span>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_newcateogry" CssClass="form-control" Width="250px" runat="server" />
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="modal-footer" style="text-align: center">
                    <button id="btnSave1" type="button" class="btn btn-primary" data-dismiss="modal">
                        Save</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">
                        Close</button>
                </div>
            </div>
        </div>
    </div>
</div>
<div>
    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">
        Add New Quantity</button>
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog" style="width: 400px;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        &times;</button>
                    <h4 class="modal-title">
                        Add New Quantity</h4>
                </div>
                <div class="modal-body">
                    <table>
                       
                        <tr>
                            <td>
                                <span class="text-danger">Unit ID: </span>
                            </td>
                            <td>
                                <asp:TextBox ID="txtPassword" CssClass="form-control" Width="250px" runat="server" ReadOnly="true"/>
                            </td>
                        </tr>
                         <tr>
                            <td>
                                <span class="text-danger">Quantity: </span>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_qty" CssClass="form-control" Width="250px" runat="server" />
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="modal-footer" style="text-align: center">
                    <button id="btnSave" type="button" class="btn btn-primary" data-dismiss="modal">
                        Save</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">
                        Close</button>
                </div>
            </div>
        </div>
    </div>
</div>

 

    [WebMethod]
        public static string UserDetails(UserDetail UserDetail)
        {
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO tblunitmaster VALUES(@unitid,@unittype)"))
                {
                    cmd.Parameters.AddWithValue("@unitid", UserDetail.unitid);
                    cmd.Parameters.AddWithValue("@unittype", UserDetail.unittype);
               
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            return "Data Saved Successfully";
        }

        public class UserDetail
        {
            public string unitid { get; set; }
            public string unittype { get; set; }
          
        }
        [WebMethod]
        public static string UserDetails1(UserDetail1 UserDetail1)
        {
            string constr1 = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr1))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO tblmcategory VALUES(@catid,@mprodcat)"))
                {
                    cmd.Parameters.AddWithValue("@catid", UserDetail1.catid);
                    cmd.Parameters.AddWithValue("@mprodcat", UserDetail1.mprodcat);
           
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            return "Data Saved Successfully";
        }

        public class UserDetail1
        {
            public string catid { get; set; }
            public string mprodcat { get; set; }

        }

 

You are viewing reply posted by: AnandM on Sep 22, 2017 05:50 AM.
Posted on Sep 22, 2017 05:50 AM
JOYSON says:
 <button id="btnSave1" type="button" class="btn btn-primary" data-dismiss="modal">
                        Save</button>

 replace above code with below

<button id="btnCategory" type="button" class="btn btn-primary" data-dismiss="modal">
    Save</button>

 

JOYSON says:
$('[id*=btnSave1]').click(function () {

 replace above with below

 $('[id*=btnCategory]').click(function () {

 

JOYSON says:
<button id="btnSave" type="button" class="btn btn-primary" data-dismiss="modal">
                        Save</button>

 replace above with below

<button id="btnQuantity" type="button" class="btn btn-primary" data-dismiss="modal">
    Save</button>

 

JOYSON says:
 $('[id*=btnSave]').click(function () {

 replace above with below

$('[id*=btnQuantity]').click(function () {