locked
My Code Works Fine In Local Computer But Hosting Its Not Saving Data But No Error RRS feed

  • Question

  • User-807418713 posted

    Hello

    I used this code in my page in local it works perfect

    Aspx.cs:
    [WebMethod]
            public static void  Insert_Data(List<MyModel> myModels)
            {
    
                foreach (var item in myModels)
                {
                    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr527"].ConnectionString);
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("Insert into Personal_Details (ItemName,Rate,Quantity) values(@ItemName,@Rate,@Quantity)", conn);
                    cmd.Parameters.AddWithValue("@ItemName", item.ItemName);
    
                    cmd.Parameters.AddWithValue("@Rate", item.Rate);
    
                    cmd.Parameters.AddWithValue("@Quantity", item.Quantity);
    
                    cmd.ExecuteNonQuery();
    
                    conn.Close();
                }
    
            }
    
            public class MyModel
            {
                public string ItemName { get; set; }
                public string Rate { get; set; }
                public string Quantity { get; set; }
            }
    Aspx:
    <script type="text/javascript">
            var data = [];
            $(document).ready(function () {
                $('#txtQuantity').blur(function (e) {
                    var ItemName = $('#txtName').val();
                    var Rate = $('#txtRate').val();
                    var Quantity = $('#txtQuantity').val
                    var item = {
                        ItemName: $('#txtName').val(),
                        Rate: $('#txtRate').val(),
                        Quantity: $('#txtQuantity').val(),
                        
                    };
                    data.push(item);
                    item.index = data.length -1;
                    $("#GridView1 tbody").append("<tr data-index='" + (data.length-1) + "'><td>" + $('#txtName option:selected').text() + "</td><td>" + $('#txtRate').val() + "</td><td>" + $('#txtQuantity').val() + "</td>" + "<td><input type='button' value='Delete' /></td>" + "</tr>")
    
                    $("#GridView1 tbody tr:last input").click(function () {
                       
                        $(this).parent("td").parent("tr").remove();
                        var index = $(this).parent("td").parent("tr").data("index");
                        data = data.filter(function (ele) {
                           
                            if (ele.index == index) {
                                return false;
                            }
                            return true;
                        })                                
                    })
    
                    $('#txtName option:first').prop("selected",true);
                    $('#txtRate').val("");
                    $('#txtQuantity').val("");
                });
    
                $("#Button1").click(function () {
    
                     $.ajax({
                        type: 'POST',
                        contentType: "application/json; charset=utf-8",
                         url: 'WebForm5555.aspx/Insert_Data',
                         data: JSON.stringify({myModels:data}),                   
                         success: function (e) {
                             alert("success")
                        }
                        
                    }); 
            })         
            });    
        </script>
    <div>
                <asp:DropDownList ID="txtName" runat="server" AppendDataBoundItems="True" DataSourceID="SqlDataSource2" DataTextField="Item_Name" DataValueField="Price">
                    <asp:ListItem>--select item--</asp:ListItem>
                </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT [Id], [Item_Name], [Price] FROM [Test3]"></asp:SqlDataSource>
                <asp:TextBox ID="txtRate" runat="server" ClientIDMode="Static"> </asp:TextBox>
                <asp:TextBox ID="txtQuantity" runat="server" ClientIDMode="Static"> </asp:TextBox>
    
                <hr />
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                    <Columns>
                        <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />
                        <asp:BoundField DataField="Rate" HeaderText="Rate" SortExpression="Rate" />
                        <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
                        <asp:CommandField ShowDeleteButton="True" HeaderText="Delete" ButtonType="Button" />
                    </Columns>            
                </asp:GridView>  
                    <input type="button" id="Button1" value="Save" />
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT [ItemName], [Rate], [Quantity] FROM [Personal_Details]"></asp:SqlDataSource>                                
            </div>

    But after i hosted in severit shows alert perfect but data not inserting into database why..?

    Saturday, June 8, 2019 10:17 AM

Answers

  • User-1174608757 posted

    Hi Gopi.MCA,

    According to your description, I have tested your code on my local machine.  I find it works well and when I refresh the page it will show the new data I insert. So I suggest that you could try below ways.

    1. I find your application use ajax, it will not directly show the data you insert but you should refresh it firstly. So have you refresh the page  and see whether it will shows data you inserted?

    2.I suggest that you could remote debug your web application with Visual Studio in your local machine by following the steps in the link. You could use worker processes to trace the process in application so that you could know why it didn't insert the data.

    Here is the link,I hope it could help you.

    https://github.com/BrandoZh/IIS-Troubleshoot/wiki/Remote-debug-with-Visual-Studio

    3.Since it has shown success , so could you please tell me where do you host the database? I think may be some one has the rights to modify your database if your sqlserver database is hosting on other server.

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 10, 2019 6:17 AM

All replies

  • User475983607 posted

    Are there any errors in the Dev Tools console?    The method does not notify the client if records are inserted so there is no way to know if the loop was entered.  

    Did you change the DB connection string?  Are you verifying the records in the same DB as defined in the connection string?

    Saturday, June 8, 2019 1:18 PM
  • User-807418713 posted

    Hello

    Is that any thing to add in web-config for hosting side..?

    Its shows perfect alert message record saved but no record is inserted in database table?

    Please Help me to solve this

    Thanks

    Sunday, June 9, 2019 5:43 AM
  • User-1174608757 posted

    Hi Gopi.MCA,

    According to your description, I have tested your code on my local machine.  I find it works well and when I refresh the page it will show the new data I insert. So I suggest that you could try below ways.

    1. I find your application use ajax, it will not directly show the data you insert but you should refresh it firstly. So have you refresh the page  and see whether it will shows data you inserted?

    2.I suggest that you could remote debug your web application with Visual Studio in your local machine by following the steps in the link. You could use worker processes to trace the process in application so that you could know why it didn't insert the data.

    Here is the link,I hope it could help you.

    https://github.com/BrandoZh/IIS-Troubleshoot/wiki/Remote-debug-with-Visual-Studio

    3.Since it has shown success , so could you please tell me where do you host the database? I think may be some one has the rights to modify your database if your sqlserver database is hosting on other server.

    Best Regards

    Wei

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 10, 2019 6:17 AM