Click here to Skip to main content
15,126,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two table one is supplier table another is product table..

inline i have taken html input and html button
used JavaScript.

and code behind
public static string UpdateStaff(string Supempdata)
    {
        bool status;

        var serializeData = JsonConvert.DeserializeObject<List<Supplier>>(Supempdata);
        try
        {

          
            using (var con = new SqlConnection(Constr))
            {
                foreach (var data in serializeData)
                {
                    using (var cmd = new SqlCommand("insert into purchase values(@VoucherNo,@PurchasedateDate,@Suppliername,@Suppaddress,@SupContactno)"))
                    {
                       
                        cmd.CommandType = CommandType.Text;

                        
                        cmd.Parameters.Add("@VoucherNo", data.Contact);
                        cmd.Parameters.Add("@PurchasedateDate", data.EmailId);
                        cmd.Parameters.Add("@Suppliername", data.Contact);
                        cmd.Parameters.Add("@Suppaddress", data.Salary);
                        cmd.Parameters.Add("@SupContactno", data.Degnation);

                        cmd.Connection = con;
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            return null;


        }
        catch (Exception ee)
        {

        }
        return null;
    }

    [WebMethod]
    public static string SaveData(string empdata)
    {
        var serializeData = JsonConvert.DeserializeObject<List<Employee>>(empdata);
        using (var con = new SqlConnection(Constr))
        {
            foreach (var data in serializeData)
            {
                using (var cmd = new SqlCommand("insert into purchase1 values(@ItemName,@batchno,@expdate,@stock,@Qty,@Rate,@total)"))
                {

                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.AddWithValue("@ItemName", data.FName);
                    cmd.Parameters.AddWithValue("@batchno", data.LName);
                    cmd.Parameters.AddWithValue("@expdate", data.EmailId);
                    cmd.Parameters.AddWithValue("@stock", data.STOCK);
                    cmd.Parameters.AddWithValue("@Qty", data.QUANTITY);
                    cmd.Parameters.AddWithValue("@Rate", data.RATE);
                    cmd.Parameters.AddWithValue("@total", data.TOTAL);

                    cmd.Connection = con;
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        return null;
    }
}


What I have tried:

<pre>      <table class="table" id="maintable">
                <thead>
                    <tr class="data-contact-person">
                        <th> </th>
                        <div class="table-toolbar">
                            <div class="row">
                                <input type="hidden" runat="server" id="mystaffid" />
                                <div class="col-md-12">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Purchase ID</label>
                                            <input type="text" id="staff_fullname" value="" placeholder="Full Name" runat="server" class="form-control Name" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Voucher No</label>
                                            <input type="text" id="staff_contact" placeholder="Contact Number" runat="server" class="form-control Contact" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Supplier Name</label>

                                            <input type="text" id="email_address" placeholder="Eg:- info@admin.com" runat="server" class="form-control Email" />
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-12">


                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Address</label>
                                            <input type="text" id="salary_details" placeholder="Enter Salary" runat="server" class="form-control Salary" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Contact no</label>
                                            <input type="text" id="designation" placeholder="Enter Designation" runat="server" class="form-control Designation" />

                                        </div>
                                    </div>
                                    <th> </th>
                                    <th> </th>
                                    <th> </th>

                                    <th> </th>
                    </tr>
                    <tr>
                        <th>ITEMNAME</th>
                        <th>BATCHNO</th>
                        <th>EXPIRYDATE</th>
                        <th>STOCK</th>
                        <th>QTY</th>
                        <th>RATE</th>

                        <th>TOTAL</th>
                     
                    </tr>
                </thead>
                <tbody>
                    <tr class="data-contact-person">
                        <td>
                            <input type="text" name="f-name" class="form-control f-name01" /></td>
                        <td>
                            <input type="text" name="l-name" class="form-control l-name01" /></td>
                        <td>
                            <input type="text" name="email" class="form-control email01" /></td>
                        <td>
                            <input type="text" name="stock" class="form-control stock01" /></td>
                         <td>
                            <input type="text" name="qty" class="form-control qty01" /></td>
                        <td>
                            <input type="text" name="rate" class="form-control rate01" /></td>
                       <td>
                            <input type="text" name="total" class="form-control total01" /></td>
                        <td>

                            <button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>
                        </td>
                    </tr>
                </tbody>
            </table>

            <%--<asp:Button ID="btnSubmit" class="btn btn-primary btn-md pull-right btn-sm col-md-4 text-center" runat="server" Text="Submit" />--%>
            <button  id="getAllEmp()">Submit1</button>
            <button id="getAllEmpData()">Submit</button>



        </div>
    </form>
</body>
<script type="text/javascript">
    $(document).ready(function () {
        $(document).on("click", ".classAdd", function () { //on is used for getting click event for dynamically created buttons

            var rowCount = $('.data-contact-person').length + 1;
            var contactdiv = '<tr class="data-contact-person">' +
                '<td><input type="text" name="f-name' + rowCount + '" class="form-control f-name01" /></td>' +
                '<td><input type="text" name="l-name' + rowCount + '" class="form-control l-name01" /></td>' +
                '<td><input type="text" name="email' + rowCount + '" class="form-control email01" /></td>' +
                '<td><input type="text" name="stock' + rowCount + '" class="form-control stock01" /></td>' +
                '<td><input type="text" name="qty' + rowCount + '" class="form-control qty01" /></td>' +
                '<td><input type="text" name="rate' + rowCount + '" class="form-control rate01" /></td>' +
                '<td><input type="text" name="total' + rowCount + '" class="form-control total01" /></td>' +
               
                '<td><button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>' +
                '<button type="button" id="btnDelete" class="deleteContact btn btn btn-danger btn-xs">Remove</button></td>' +
                '</tr>';
            $('#maintable').append(contactdiv); // Adding these controls to Main table class
        });

        $(document).on("click", ".deleteContact", function () {
            $(this).closest("tr").remove(); // closest used to remove the respective 'tr' in which I have my controls 
        });

        function getAllEmpData() {
            console.log("getAllEmpData");
            var data = [];
            $('tr.data-contact-person').each(function () {
                var firstName = $(this).find('.f-name01').val();
                var lastName = $(this).find('.l-name01').val();
                var emailId = $(this).find('.email01').val();
                var Stock = $(this).find('.stock01').val();
                var qty = $(this).find('.qty01').val();
                var rate = $(this).find('.rate01').val();
                var totalamount = $(this).find('.total01').val();
               

                var alldata = {
                    'FName': firstName,
                    'LName': lastName,
                    'EmailId': emailId,
                    'STOCK': Stock,
                    'QUANTITY': qty,
                    'RATE': rate,          
                   'TOTAL': totalamount
                    
                }
                data.push(alldata);
            });
            console.log(data);
            return data;
        }

        $("#btnSubmit").click(function () {
            var data = JSON.stringify(getAllEmpData());
            //console.log(data);
            $.ajax({
                url: 'Home.aspx/SaveData',
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ 'Supempdata': data }),
                success: function () {
                    alert("Data Added Successfully");
                },
                error: function () {
                    alert("Error while inserting data");
                }
            });
        });
    });
</script>

<script type="text/javascript">
    $(document).ready(function () {
        $(document).on("click", ".classAdd", function () { //on is used for getting click event for dynamically created buttons

            var rowCount = $('.data-contact-person').length + 1;
            var contactdiv = '<tr class="data-contact-person">' +
                '<td><input type="text" name="f-name' + rowCount + '" class="form-control f-name01" /></td>' +
                '<td><input type="text" name="l-name' + rowCount + '" class="form-control l-name01" /></td>' +
                '<td><input type="text" name="email' + rowCount + '" class="form-control email01" /></td>' +
                '<td><input type="text" name="stock' + rowCount + '" class="form-control stock01" /></td>' +
                '<td><input type="text" name="qty' + rowCount + '" class="form-control qty01" /></td>' +
                '<td><input type="text" name="rate' + rowCount + '" class="form-control rate01" /></td>' +
                '<td><input type="text" name="total' + rowCount + '" class="form-control total01" /></td>' +

                '<td><button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>' +
                '<button type="button" id="btnDelete" class="deleteContact btn btn btn-danger btn-xs">Remove</button></td>' +
                '</tr>';
            $('#maintable').append(contactdiv); // Adding these controls to Main table class
        });

        $(document).on("click", ".deleteContact", function () {
            $(this).closest("tr").remove(); // closest used to remove the respective 'tr' in which I have my controls 
        });

        var getAllEmp;

        $( document ).ready(function() {
            getAllEmp = function () {
                console.log(" getAllEmp");
                var data = [];
                $('tr.data-contact-person').each(function () {
                    var firstName = $(this).find('.Name').val();
                    var Con = $(this).find('.Contact').val();
                    var emailId = $(this).find('.Email').val();
                    var Sal = $(this).find('.Salary').val();
                    var Deg = $(this).find('.Designation').val();


                    var alldata = {
                        'FName': firstName,
                        'Contact': Con,
                        'EmailId': emailId,
                        'Salary': Sal,
                        'Degnation': Deg

                    }
                    data.push(alldata);
                });
                console.log(data);
                return data;
            }

            $("#btnSubmit").click(function () {
                var data = JSON.stringify(getAllEmp());
                //console.log(data);
                $.ajax({
                    url: 'Home.aspx/UpdateStaff',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify({ 'Supempdata': data }),
                    success: function () {
                        alert("Data Added Successfully");
                    },
                    error: function () {
                        alert("Error while inserting data");
                    }
                });
            });
        });
        });
    
</script>




//Code behind code here-----------


public static string UpdateStaff(string Supempdata)
    {
        bool status;

        var serializeData = JsonConvert.DeserializeObject<List<Supplier>>(Supempdata);
        try
        {

          
            using (var con = new SqlConnection(Constr))
            {
                foreach (var data in serializeData)
                {
                    using (var cmd = new SqlCommand("insert into purchase values(@VoucherNo,@PurchasedateDate,@Suppliername,@Suppaddress,@SupContactno)"))
                    {
                       
                        cmd.CommandType = CommandType.Text;

                        
                        cmd.Parameters.Add("@VoucherNo", data.Contact);
                        cmd.Parameters.Add("@PurchasedateDate", data.EmailId);
                        cmd.Parameters.Add("@Suppliername", data.Contact);
                        cmd.Parameters.Add("@Suppaddress", data.Salary);
                        cmd.Parameters.Add("@SupContactno", data.Degnation);

                        cmd.Connection = con;
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            return null;


        }
        catch (Exception ee)
        {

        }
        return null;
    }

    [WebMethod]
    public static string SaveData(string empdata)
    {
        var serializeData = JsonConvert.DeserializeObject<List<Employee>>(empdata);
        using (var con = new SqlConnection(Constr))
        {
            foreach (var data in serializeData)
            {
                using (var cmd = new SqlCommand("insert into purchase1 values(@ItemName,@batchno,@expdate,@stock,@Qty,@Rate,@total)"))
                {

                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.AddWithValue("@ItemName", data.FName);
                    cmd.Parameters.AddWithValue("@batchno", data.LName);
                    cmd.Parameters.AddWithValue("@expdate", data.EmailId);
                    cmd.Parameters.AddWithValue("@stock", data.STOCK);
                    cmd.Parameters.AddWithValue("@Qty", data.QUANTITY);
                    cmd.Parameters.AddWithValue("@Rate", data.RATE);
                    cmd.Parameters.AddWithValue("@total", data.TOTAL);

                    cmd.Connection = con;
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        return null;
    }
}
Posted
Updated 21-May-19 3:56am
Comments
F-ES Sitecore 21-May-19 8:21am
   
What's your question?
[no name] 21-May-19 8:25am
   
I want to store the data in two different tables when we click the button , I am using JavaScript and my code is in asp.net. one table supplier second one is product table.
phil.o 21-May-19 8:21am
   
What is the problem?
[no name] 21-May-19 8:24am
   
I want to store the data in two different tables when we click the button , I am using JavaScript and my code is in asp.net. one table supplier second one is product table. i am developing invoice project
phil.o 21-May-19 8:29am
   
Yes, that is what you already stated in your "question".
Problem is, you only gave your requirements and the code you have. We also need to know what is the issue: is there an exception thrown? an error message? or is it just that it behaves differently from what you are expecting?
[no name] 21-May-19 8:33am
   
getting error uncaught Refrence error: getAllempdata() is not defined.html button event.onclick
F-ES Sitecore 21-May-19 8:37am
   
You have a function as the id which makes no sense. Change it to something like

<button id="btnSubmit">Submit</button>
lmoelleb 21-May-19 8:24am
   
At this point you have to start debugging. If you don't know how to use a debugger, you need to learn it. It is the single most important skill for a programmer. Also remove empty catch statement. How are you supposed to find out why things are not working when you ignore errors?
#realJSOP 21-May-19 8:42am
   
If you had actually "tried everything", your code would be working.

1 solution

Why are you using function name as ID for your HTML button instead you need to user method with onclick event to perform the action. Try with below -

HTML
<button id="btnGetAllEmp" onclick="getAllEmp()">Submit1</button>
<button id="btnSubmit" onclick="getAllEmpData()">Submit</button>
   

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