Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have write these codes but what is the problem with these data not save in database.
Please help me...
Tahnk you
Here is my Asp code
ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ContactInfo.aspx.cs" Inherits="AspjQuerySaveData.ContactInfo1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
    <script>
        $(document).ready(function () {
            $('#btnsave').click(function () {
                var contactName = $('#<%=txtContactName.ClientID%>').val();
                var contactNo = $('#<%=txtContactNo.ClientID%>').val();
                $('#LoadingPanel').show();
            });
            $.ajax({
                url: "ContactInfo.aspx/SaveData",
                type: "POST",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: true,
                cache: false,
                data: JSON.stringify({
                    "contactName":contactName,
                    "contactNo": contactNo
                }),
                success: function (data) {
                    if (data.d == "success") {
                        alert("Data Saved Successfully");
                        $('#<%=txtContactName.ClientID%>').val('');
                        $('#<%=txtContactNo.ClientID%>').val('');
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError);
                }
            }).done(function () {
                $('#LoadingPanel').hide()
            })
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <div>
        <table>
            <tr>
                <td>Contact Name:</td>
                <td><asp:TextBox runat="server" Width="200px" ID="txtContactName" /></td>
            </tr>
            <tr>
                <td>Contact Number:</td>
                <td><asp:TextBox runat="server" ID="txtContactNo" Width="200px" /></td>
            </tr>
            <tr>
                <td><input type="button" id="btnsave" style="width:200px;" value="Submit" /></td>
                <td><div id="LoadingPanel" style="display:none; font-weight:bold;" >Data Saving ...</div></td>
            </tr>
        </table>
    </div>
</asp:Content>

And C#
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace AspjQuerySaveData
{
    public partial class ContactInfo1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        [WebMethod]
        public static string SaveData(string contactName, string contactNo)
        {
            string status = "";
            ContactInfo c = new ContactInfo {Id=0,ContactName=contactName,ContactNo=contactNo};
            using (DatabaseEntities dc = new DatabaseEntities())
            {
                dc.ContactInfoes.Add(c);
                dc.SaveChanges();
                return status;
            }
        }
    }
}
Posted
Comments
Herman<T>.Instance 12-Aug-14 4:05am    
Where is a date in your
ContactInfo c = new ContactInfo {Id=0,ContactName=contactName,ContactNo=contactNo};
?
m-e-h-d-h-i 12-Aug-14 4:07am    
I have create ado.net entity framework that connect to database
m-e-h-d-h-i 12-Aug-14 4:09am    
ContactInfo is databse table name
m-e-h-d-h-i 12-Aug-14 4:12am    
I have no problem with my c# codes because when i call Savedata function in pageload it works
but when i call SaveData in browser nothing happen
Jameel VM 12-Aug-14 4:53am    
where you are facing problem, in ajax call or inside service method?

1 solution

Please declare the two variables out side the function like below

C#
var contactName ="";
var contactNo = "";
$('#btnsave').click(function () {
                contactName = $('#<%=txtContactName.ClientID%>').val();
                contactNo = $('#<%=txtContactNo.ClientID%>').val();
                $('#LoadingPanel').show();
            });


Hope this helps
 
Share this answer
 
Comments
m-e-h-d-h-i 12-Aug-14 8:15am    
I have correct last error but another error
POST http://localhost:13978/ContactInfo.aspx/SaveData 500 (Internal Server Error)
Jameel VM 13-Aug-14 1:47am    
did you try to browse the url directly in browser?

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