|
here's the problem
I've got an API with the following address (HTTP post)
http:
"headers": {
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"username": "mrkeivan",
"password": "09126101185",
"uiId": "d960a994-0972-44ba-ae2c-2c3a01e135c1"
}
I need to call this api in another website and a mobile application, to do so I enabled Asp.net APi Cores globally in web config and WebApiConfig.cs
**in WebApiConfig.cs**
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
> as well as Web.config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST, PUT, DELETE, GET, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="content-Type, accept, origin, X-Requested-With, Authorization, name" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
when I call this in postman, it works perfectly
however when I call it through an ajax method or a friend of mine calls it in his mobile app project it returns the following error
> XMLHttpRequest cannot load
> http://api.novin.solutions/api/member/login. Response for preflight
> has invalid HTTP status code 404 OPTIONS
> http://api.novin.solutions/api/member/login 404 (Not Found)
I have read many articles in this matter and applied all the suggestions, but nothing fixed my problem
any help would be appreciated
|
|
|
|
|
When I made a call to that api this is the response I got
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, PUT, DELETE, GET, OPTIONS
Access-Control-Allow-Headers: content-Type, accept, origin, X-Requested-With, Authorization, name
Access-Control-Allow-Credentials: true
Date: Thu, 09 Mar 2017 10:02:31 GMT
Content-Length: 135
Note that there are multiple "Access-Control-Allow-Origin" headers. The first thing I would do is look to why that is happening and get it down to one as the browser may well reject the response as invalid (if I use IE I get an error in the console explicitly stating there are duplicate headers). This is probably happening as you are configuring it in multiple places.
|
|
|
|
|
Hi, thanx for the response
removed them, now I only have it in
- web.config and
- config.EnableCors(); in WebApiConfig
|
|
|
|
|
Now there are two
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Thu, 09 Mar 2017 11:15:52 GMT
Content-Length: 135
You only need it defined once, but you are defining it in two config files
|
|
|
|
|
How do you make this call ???
|
|
|
|
|
Via ajax and I use Fiddler to examine the response
$(document).ready(function () {
$.ajax({
type: "POST",
url: "http://api.novin.solutions/api/member/login",
data: {
"username": "mrkeivan",
"password": "09126101185",
"uiId": "d960a994-0972-44ba-ae2c-2c3a01e135c1"
}
}).done(function (data) {
alert(data);
}).fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
});
|
|
|
|
|
you are right, i found out a line I left in the global.cs
thanx man, appreciate it.
|
|
|
|
|
this is what I get
Request
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:cache-control
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:api.novin.solutions
Origin:http://localhost:1344
Pragma:no-cache
Referer:http://localhost:1344/Home/Index
User-Agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Response
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:content-Type, accept, origin, X-Requested-With, Authorization, name
Access-Control-Allow-Methods:POST, PUT, DELETE, GET, OPTIONS
Access-Control-Allow-Origin:*
Connection:close
Content-Length:1245
Content-Type:text/html
Date:Fri, 10 Mar 2017 05:26:20 GMT
Server:Microsoft-IIS/8.5
Vary:Accept-Encoding
X-Powered-By:ASP.NET
X-Powered-By-Plesk:PleskWin
|
|
|
|
|
If that's your real password that you've just posted on a public forum, I hope it doesn't provide access to any sensitive data, and that you're going to change it as soon as the problem is resolved.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
))) don't worry it's a sample ! 
|
|
|
|
|
hello everyone
currently i am doing a project on XSS(Cross Site Scripting) prevention, for that i want to process a small text paragraph, contain text and java code.
example-
The Conjuring 2 is a mixed bag of a movie.....That being said, if you are someone that doesn't mind jump scares, you will most likely eat this movie up!<..........some java code here...........>
i want to separate out the text code present in between "<" and ">" so that i can process it separately.
thank you for your help.
|
|
|
|
|
You could use a regex or the DOM model to find the specific tags you are interested in.
|
|
|
|
|
|
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);
});
$(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);
});
$(document).on("click", ".deleteContact", function () {
$(this).closest("tr").remove();
});
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());
$.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.
|
|
|
|
|
You fail to check the return value from your call to cmd.ExecuteNonQuery() , so you have no way of knowing whether that succeeded or not. In your ajax calls, where do the success or failure statuses come from, and what other status information is available?
|
|
|
|
|
Hi Richard,
Many thanks for attempting to help.
My experience with ajax call and JSON object WebMethods() tells me that you can't get to codebehind unless you get beyond the alert() message.
I could be wrong so far but to answer your question, I think that using
console.log(data); tells me what values are being passed to getAllEmpData and then to #btnsubmit control.
For instance, when I tried to use firebug to inspect the code, I got the following:
SCRIPT16389: Unspecified error.
[object Object],[object Object]
[{},{"mySource":"Martin Short","mySAddress":"1 Hollywood Way","mySIncome":"3401221"}]
This tells me that returning
[object Object],[object Object] is a problem somewhere with function because the correct information I entered from markup is captured and displayed as JSON objects below
[object Object],[object Object] .
I just don't know what is causing it.
Hope I am making sense.
|
|
|
|
|
The trouble is that message tells you (and us) nothing useful. And I am afraid I don't know AJAX, so cannot suggest anything on that score. Obviously adding some logging information should go some way to helping you to a solution.
|
|
|
|
|
Ok,
I will keep at it.
I would have preferred to NOT go this route of using JSON and Ajax calls but I have not been able to figure out a way to continue using the code Richard D wrote for using AngularJS with the PHP bit.
Thanks for your help though.
|
|
|
|
|
I'd start by using Fiddler or the browser's dev tools to see what is being passed to and from your webmethod as that can often help tracking down issues, and if your server code is throwing an exception it often lets you see what that exception is
Using the browser's dev tools to diagnose ajax problems (and other things) | The ASP.NET Forums[^]
I'd also debug the webmethod at the project to inspect the data come into the method and to see if the deserialisation is working or if the code is throwing exceptions.
|
|
|
|
|
I am still pretty stumped by this issue.
I have moved past the initial problem.
Right now, when i run the app, it is inserting records to the database correctly.
However, this works for only one class.
When I add a second class or table, I start getting a very deceitful error message.
For instance, when I run the app with one class:
It works.
When I run the second class separately too, it works as well.
However, when I combine them which is the real intent of this project, I keep getting "parameter @name is required but not supplied"
I have tested the stored procedure that I am using in Sql Server Management Studio and it works fine.
There is one critical step that I am missing but not sure how to fix this. I wonder if it is not possible to have multiple INSERT statements using Ajax and webmethods.
Below is the code that I am currently using.
<script type="text/javascript">
$(document).ready(function () {
$(document).on("click", "#empAdd", function () {
var rowCount = $('.data-contact-person').length + 1;
var contactdiv = '<tr class="data-contact-personm">' +
'<td><input type="text" style="width:200px;" name="employeename' + rowCount + '" placeholder="Your name..." class="form-control employeename01" /></td>' +
'<td><input type="text" style="width:200px;" name="employeetitle' + rowCount + '" placeholder="Your title..." class="form-control employeetitle01" /></td>' +
'<td><input type="text" style="width:200px;" name="employeeemail' + rowCount + '" placeholder="Your email address..." class="form-control employeeemail01" /></td>' +
'<td style="width:200px;"><button type="button" id="empAdd" class="btn btn-xs btn-primary classAdd">Add More</button>' +
'<button type="button" id="empDeletem" class="deleteContact btn btn btn-danger btn-xs">Remove</button></td>' +
'</tr>';
$('#emptable').append(contactdiv);
});
$(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 + '" placeholder="Name of income source..." class="form-control sourcename01" /></td>' +
'<td><input type="text" style="width:200px;" name="sourceaddress' + rowCount + '" placeholder="Address of income source..." class="form-control sourceaddress01" /></td>' +
'<td><input type="text" style="width:200px;" name="sourceincome' + rowCount + '" placeholder="Income..." 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);
});
$(document).on("click", ".deleteContact", function () {
$(this).closest("tr").remove();
});
function getAllSourcepData() {
var data = [];
$('tr.data-contact-person').each(function () {
var ename = $(this).find('.employeename01').val();
var etitle = $(this).find('.employeetitle01').val();
var email = $(this).find('.employeeemail01').val();
var sname = $(this).find('.sourcename01').val();
var saddress = $(this).find('.sourceaddress01').val();
var sincome = $(this).find('.sourceincome01').val();
var alldata = {
'emplName': ename,
'emplTitle': etitle,
'empMail': email,
'mySource': sname,
'mySAddress': saddress,
'mySIncome': sincome
}
data.push(alldata);
});
console.log(data);
return data;
}
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllEmpData());
var data = JSON.stringify(getAllSourcepData());
console.log(data);
$.ajax({
url: 'disclosures.aspx/SaveData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
});
});
</script>
<style type="text/css">
.bs-example{
margin-left: 250px;
margin-top: 30px;
}
</style>
</head>
<body>
<div class="bs-example">
<form id="form1" runat="server">
<div class="container">
<table id="emptable">
<thead>
<tr>
<th>Employee Name</th>
<th>Title</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr class="data-contact-person">
<td>
<input type="text" style="width:200px;" name="employeename" class="form-control employeename01" placeholder="Your name..." /></td>
<td>
<input type="text" style="width:200px;" name="employeetitle" class="form-control employeetitle01" placeholder="Your title..." /></td>
<td>
<input type="text" style="width:200px;" name="employeeemail" class="form-control employeeemail01" placeholder="Your email address..." /></td>
<td>
</td>
</tr>
</tbody>
</table><br /><br />
<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" placeholder="Name of income source..." /></td>
<td>
<input type="text" style="width:200px;" name="sourceaddress" class="form-control sourceaddress01" placeholder="Address of income source..." /></td>
<td>
<input type="text" style="width:200px;" name="sourceincome" class="form-control sourceincome01" placeholder="Income..." /></td>
<td style="width:200px;">
<button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>
</td>
</tr>
</tbody>
</table><br /><br />
<button type="button" id="btnSubmit" class="btn btn-primary btn-md pull-center btn-sm">Submit</button>
</div>
</form>
Below is code behind:
<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("sprInsertDisclsosures")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@ename", data.emplName)
cmd.Parameters.AddWithValue("@email", data.emplTitle)
cmd.Parameters.AddWithValue("@title", data.empMail)
cmd.Parameters.AddWithValue("@sname", data.mySource)
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 emplName() As String
Get
Return m_empName
End Get
Set(value As String)
m_empName = value
End Set
End Property
Private m_empName As String
Public Property emplTitle() As String
Get
Return m_empTitle
End Get
Set(value As String)
m_empTitle = value
End Set
End Property
Private m_empTitle As String
Public Property empMail() As String
Get
Return m_empMail
End Get
Set(value As String)
m_empMail = value
End Set
End Property
Private m_empMail As String
Public Property mySource() As String
Get
Return m_mySource
End Get
Set(value As String)
m_mySource = value
End Set
End Property
Private m_mySource 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
|
|
|
|
|
Look at the sprInsertDisclsosures SP, does it define a "@name" parameter? If so look at the list of params you are adding to the command, none define @name.
You also have nested document.ready functions in your javascript, not sure that is going to work. Define a single document.ready and put everything you need into it.
|
|
|
|
|
Hi,
As stated in my last post, I tested the stored proc separately and it works a treat.
So, those params match exactly the ones you see in my code.
That's why I said it is showing deceitful error message.
Normally, that error would indicate that the @parameter is either not supplied or misspelled.
None of this is the case.
All of this struggle is because I am trying to create about 4 dynamic rows each with its own Add More button like the screenshot below:
Dynamic Rows[^]
I was able to do this in PHP with the help of Richard Deeming but management wants it done asp.net after those exhaustive efforts doing it in PHP.
I have scoured everywhere on the web looking for examples but I can find just one with one Add More Row button.
|
|
|
|
|
Computers don't make mistakes, but people do If I had a penny for every time a developer swore they hadn't misspelled something only for me to look and realise they had, or were looking at the wrong SP etc, I'd probably have £3.82.
|
|
|
|
|
Quote:
var contactdiv = '<tr class="data-contact-personm">' +
Is that a typo in your question? Based on the rest of your code, the class should be data-contact-person , without the trailing "m".
samflex wrote: parameter @name is required but not supplied
Are you absolutely sure the error message mentions the @name parameter? As F-ES Sitecore said, you're not passing a parameter called @name to the stored procedure, so if that's a required parameter, it would explain the error.
Alternatively, if the error is referring to one of the parameters you have defined, it's probably because one of the properties is Nothing . If you pass Nothing to the command parameter, the parameter will use its default value as defined in the stored procedure. If it doesn't have a default value, you'll get the "parameter not supplied" error.
The simplest solution would be to set a default value on the parameters of your stored procedure:
ALTER PROC sprInsertDisclsosures
(
@ename nvarchar(50) = Null,
@email nvarchar(200) = Null,
@title nvarchar(20) = Null,
@sname nvarchar(50) = Null,
@saddress nvarchar(max) = Null,
@sincome varchar(10) = Null
)
As
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard,
First of all, just to make sure I have all my information out there, this is where I got the code i am currently using.
Add And Delete HTML Table Rows Dynamically Using ASP.NET[^]
I just modified to what it is right now.
No, that is not a typo.
Those are being called with the ajax call like:
function getAllEmpData() {
var data = [];
$('tr.data-contact-personm').each(function () {
var ename = $(this).find('.employeename01').val();
var etitle = $(this).find('.employeetitle01').val();
var email = $(this).find('.employeeemail01').val();
var alldata = {
'emplName': ename,
'emplTitle': etitle,
'empMail': email
}
data.push(alldata);
});
console.log(data);
return data;
}
Then
function getAllEmpData() {
var data = [];
$('tr.data-contact-person').each(function () {
function getAllEmpData() {
var data = [];
$('tr.data-contact-person2').each(function () {
etc.
In other words, any unique Add More button has its own row as in tr.data-contact-person. At least that's how I understand.
As for the error message, sorry it mentions @sname which is the alias for sourcename.
If I run these dynamic rows individually, they work just fine.
First instance, if I run the row that contains employeename, employeetitle and employeeincome, inserts correctly into the database.
Similarly, if I run the row that contains sourcename (@sname), sourceaddress, and sourceincome, it works just fine.
It is when I try to combine them that I get the error.
I suspect that ajax calls would not allow multiple INSERT statements.
As for the stored proc, I have it declared correctly. I wish that was the source of the error that I am having. I would have resolved it by now.
This is the stored proc just so you know:
CREATE PROCEDURE [dbo].[sprInsertDisclsosures]
@ename varchar(50),
@title varchar(50),
@email varchar(50),
@sname varchar(150),
@saddress varchar(100),
@sincome numeric(18,2)
as
BEGIN
SET NOCOUNT OFF;
INSERT INTO EMPLOYEES (employeeName, empTitle, email) Values (@ename,@title,@email);
declare @ID int
set @ID = SCOPE_IDENTITY()
if @@rowcount > 0
INSERT INTO SourceDetails (sourcename,sourceaddress, sourceincome,EmployeeID) Values (@sname, @saddress,@sincome, @ID);
END
If I test it from SSMS, it inserts records into Employees table, gets the last inserted ID and inserts into SourceDetails table. So, I don't think there is an issue here.
modified 8-Mar-17 14:51pm.
|
|
|
|
|