Click here to Skip to main content
15,887,596 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Api post call works on postman but in mobile app or ajax call it doesn't Pin
mrkeivan9-Mar-17 23:35
mrkeivan9-Mar-17 23:35 
GeneralRe: Api post call works on postman but in mobile app or ajax call it doesn't Pin
mrkeivan9-Mar-17 18:54
mrkeivan9-Mar-17 18:54 
SuggestionRe: Api post call works on postman but in mobile app or ajax call it doesn't Pin
Richard Deeming9-Mar-17 1:18
mveRichard Deeming9-Mar-17 1:18 
GeneralRe: Api post call works on postman but in mobile app or ajax call it doesn't Pin
mrkeivan9-Mar-17 17:57
mrkeivan9-Mar-17 17:57 
QuestionASP.NET C# -STRING PROCESSING Pin
batman568-Mar-17 18:04
batman568-Mar-17 18:04 
SuggestionRe: ASP.NET C# -STRING PROCESSING Pin
Richard MacCutchan8-Mar-17 21:17
mveRichard MacCutchan8-Mar-17 21:17 
AnswerRe: ASP.NET C# -STRING PROCESSING Pin
Richard Deeming9-Mar-17 1:15
mveRichard Deeming9-Mar-17 1:15 
QuestionError Inserting data - into the database. Pin
samflex6-Mar-17 3:28
samflex6-Mar-17 3:28 
Back for more help, sorry.

After all that struggle to get my project working in php, they came back on Wednesday last week that they could not use current project written in php because the wordpress theme that the php would be integrated with is not set up correctly to allow us the ability to upload the file as a template and we could gain access to MySQL database.

So, I had to start all over building the app in asp.net.

I have been working on this since Wednesday last week.

For the most part, I think I got most it working. I was able to rewrite the dynamic row addition.

I am currently trying to use JSON object with an ajax call to submit the records into the database.

The issue right now, however, is that I get, my custom error message which says, "Error while inserting data".

Sorry for the long code. I am posting them because I am not sure what is causing the error, whether it is coming from my HTML markup or codefile.

Any ideas, as always, is greatly appreciated.

   <meta name="viewport" content="width=device-width, initial-scale=1" />  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />  
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(document).on("click", "#btnAdd", function () { //
                var rowCount = $('.data-contact-person').length + 1;
                var contactdiv = '<tr class="data-contact-person">' +
                    '<td><input type="text" style="width:200px;" name="sourcename' + rowCount + '" class="form-control sourcename01" /></td>' +
                    '<td><input type="text" style="width:200px;" name="sourceaddress' + rowCount + '" class="form-control sourceaddress01" /></td>' +
                    '<td><input type="text" style="width:200px;" name="sourceincome' + rowCount + '" class="form-control sourceincome01" /></td>' +
                    '<td style="width:200px;"><button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>' +
                    '<button type="button" id="btnDelete1" 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", "#btnAdd2", function () { //
                var rowCount = $('.data-contact-person2').length + 1;
                var contactdiv = '<tr class="data-contact-person2">' +
                    '<td><input type="text" style="width:200px;" name="spousename' + rowCount + '" class="form-control spousename01" /></td>' +
                    '<td><input type="text" style="width:200px;" name="spouseaddress' + rowCount + '" class="form-control spouseaddress01" /></td>' +
                    '<td><input type="text" style="width:200px;" name="spouseincome' + rowCount + '" class="form-control spouseincome01" /></td>' +
                    '<td><button type="button" id="btnAdd2" class="btn btn-xs btn-primary classAdd">Add More</button>' +
                    '<button type="button" id="btnDelete2" class="deleteContact btn btn btn-danger btn-xs">Add More</button></td>' +
                    '</tr>';
                $('#maintable2').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() {
                var data = [];
                $('tr.data-contact-person').each(function () {
                    var sname = $(this).find('.sourcename01').val();
                    var saddress = $(this).find('.sourceaddress01').val();
                    var sincome = $(this).find('.sourceincome01').val();
                    var spname = $(this).find('.spousename01').val();
                    var spaddress = $(this).find('.spouseaddress01').val();
                    var spincome = $(this).find('.spouseincome01').val();
                    var alldata = {
                        'mySource': sname,
                        'mySAddress': saddress,
                        'mySIncome': sincome,
                        'mySpouse': spname,
                        'mySPAddress': spaddress,
                        'mySPIncome': spincome
                    }
                    data.push(alldata);
                });
                console.log(data);
                return data;
            }

            $("#btnSubmit").click(function () {
                var data = JSON.stringify(getAllEmpData());
                //console.log(data);
                $.ajax({
                    url: 'disclosures.aspx/SaveData',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify({ 'empdata': data }),
                    success: function () {
                        alert("Data Added Successfully");
                    },
                    error: function () {
                        alert("Error while inserting data");
                    }
                });
            });
        });
    </script>
</head>
<body>  
    <form id="form1" runat="server">  
        <div class="container">  
            <h2>Forms</h2>  
            <table class="table" id="maintable">  
                <thead>  
                    <tr>  
                        <th>Name</th>  
                        <th>Address</th>  
                        <th>Income</th>  
                    </tr>  
                </thead>  
                <tbody>  
                    <tr class="data-contact-person">  
                        <td>  
                            <input type="text" style="width:200px;" name="sourcename" class="form-control sourcename01" /></td>  
                        <td>  
                            <input type="text" style="width:200px;" name="sourceaddress" class="form-control sourceaddress01" /></td>  
                        <td>  
                            <input type="text" style="width:200px;" name="sourceincome" class="form-control sourceincome01" /></td>  
                        <td style="width:200px;">  
                            <button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>  
                        </td>  
                    </tr>  
                </tbody>  
            </table> 
            
             <table class="table" id="maintable2">  
                <thead>  
                    <tr>  
                        <th>Source </th>  
                        <th>Address </th>  
                        <th>Income</th>  
                    </tr>  
                </thead>  
                <tbody>  
                    <tr class="data-contact-person2">  
                        <td>  
                            <input type="text" style="width:200px;" name="spousename" class="form-control spousename01" /></td>  
                        <td>  
                            <input type="text" style="width:200px;" name="spouseaddress" class="form-control spouseaddress01" /></td>  
                        <td>  
                            <input type="text" style="width:200px;" name="spouseincome" class="form-control spouseincome01" /></td>  
                        <td style="width:200px;">  
                            <button type="button" id="btnAdd2" class="btn btn-xs btn-primary classAdd2">Add More</button>  
                        </td>  
                    </tr>  
                </tbody>  
            </table>   
            <button type="button" id="btnSubmit" class="btn btn-primary btn-md pull-right btn-sm">Submit</button>  
        </div>  
    </form>  
</body>  
</html>

'//CodeFile
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Linq
Imports System.Web
Imports System.Web.Services
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Newtonsoft.Json

Partial Public Class disclosures
    Inherits System.Web.UI.Page
    Public Shared Constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString

    Protected Sub Page_Load(sender As Object, e As EventArgs)

    End Sub

    <WebMethod()> _
    Public Shared Function SaveData(empdata As String) As String
        Dim serializedData = JsonConvert.DeserializeObject(Of List(Of Employee))(empdata)
        Using con = New SqlConnection(Constr)
            If con.State = ConnectionState.Closed Then
                con.Open()
            End If
            For Each data As Employee In serializedData
                Using cmd = New SqlCommand("INSERT INTO SourceDetails(sourcename, sourceaddress, sourceincome, createDate) VALUES(@sname, @saddress,@sincome,@CreatedDate)")

                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@sname", data.mySpouse)
                    cmd.Parameters.AddWithValue("@saddress", data.mySAddress)
                    cmd.Parameters.AddWithValue("@sincome", data.mySIncome)
                    cmd.Parameters.AddWithValue("@CreatedDate", DateTime.Now)
                    cmd.Connection = con
                    cmd.ExecuteNonQuery()
                End Using
            Next
            con.Close()
        End Using
        Return Nothing
    End Function
End Class

Public Class Employee
    Public Property mySpouse() As String
        Get
            Return m_mySpouse
        End Get
        Set(value As String)
            m_mySpouse = value
        End Set
    End Property
    Private m_mySpouse As String
    Public Property mySAddress() As String
        Get
            Return m_mySAddress
        End Get
        Set(value As String)
            m_mySAddress = value
        End Set
    End Property
    Private m_mySAddress As String
    Public Property mySIncome() As String
        Get
            Return m_mySIncome
        End Get
        Set(value As String)
            m_mySIncome = value
        End Set
    End Property
    Private m_mySIncome As String
    Public Property CreatedDate() As DateTime
        Get
            Return m_CreatedDate
        End Get
        Set(value As DateTime)
            m_CreatedDate = Value
        End Set
    End Property
    Private m_CreatedDate As DateTime

End Class


modified 6-Mar-17 9:35am.

AnswerRe: Error Inserting data - into the database. Pin
Richard MacCutchan6-Mar-17 5:36
mveRichard MacCutchan6-Mar-17 5:36 
GeneralRe: Error Inserting data - into the database. Pin
samflex6-Mar-17 6:19
samflex6-Mar-17 6:19 
GeneralRe: Error Inserting data - into the database. Pin
Richard MacCutchan6-Mar-17 6:25
mveRichard MacCutchan6-Mar-17 6:25 
GeneralRe: Error Inserting data - into the database. Pin
samflex6-Mar-17 7:01
samflex6-Mar-17 7:01 
AnswerRe: Error Inserting data - into the database. Pin
F-ES Sitecore7-Mar-17 0:52
professionalF-ES Sitecore7-Mar-17 0:52 
GeneralRe: Error Inserting data - into the database. Pin
samflex8-Mar-17 5:03
samflex8-Mar-17 5:03 
GeneralRe: Error Inserting data - into the database. Pin
F-ES Sitecore8-Mar-17 5:44
professionalF-ES Sitecore8-Mar-17 5:44 
GeneralRe: Error Inserting data - into the database. Pin
samflex8-Mar-17 6:09
samflex8-Mar-17 6:09 
GeneralRe: Error Inserting data - into the database. Pin
F-ES Sitecore8-Mar-17 22:16
professionalF-ES Sitecore8-Mar-17 22:16 
GeneralRe: Error Inserting data - into the database. Pin
Richard Deeming8-Mar-17 7:58
mveRichard Deeming8-Mar-17 7:58 
GeneralRe: Error Inserting data - into the database. Pin
samflex8-Mar-17 8:40
samflex8-Mar-17 8:40 
GeneralRe: Error Inserting data - into the database. Pin
Richard Deeming8-Mar-17 8:50
mveRichard Deeming8-Mar-17 8:50 
GeneralRe: Error Inserting data - into the database. Pin
samflex8-Mar-17 8:55
samflex8-Mar-17 8:55 
GeneralRe: Error Inserting data - into the database. Pin
Richard Deeming8-Mar-17 9:09
mveRichard Deeming8-Mar-17 9:09 
GeneralRe: Error Inserting data - into the database. Pin
samflex8-Mar-17 9:19
samflex8-Mar-17 9:19 
Questioninput string was not in a correct format published site error when recycle working properly Pin
Praveen Kandari3-Mar-17 23:31
Praveen Kandari3-Mar-17 23:31 
AnswerRe: input string was not in a correct format published site error when recycle working properly Pin
Richard MacCutchan3-Mar-17 23:37
mveRichard MacCutchan3-Mar-17 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.