TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
rahul patil
NA
160
8.6k
how to return jsondata using webservice?
May 18 2020 7:15 AM
I want to return
json data when filled the webservice page?
issue is when click on
Invoke button Data is not displayed
By default the webmethod is post type
WebService1.asmx.cs
namespace
WebServiceDemo
{
[WebService(Namespace =
"http://tempuri.org/"
)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(
false
)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public
class
WebService1 : System.Web.Services.WebService
{
SqlConnection cn =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"cnn"
].ConnectionString);
[WebMethod]
[ScriptMethod(UseHttpGet =
true
, ResponseFormat = ResponseFormat.Json)]
public
string
InsertData(
string
fname,
string
mname,
string
lname,
string
emailid,
string
password,
string
contactno,
string
hobby,
string
address,
string
countrycodenum)
{
cn.Open();
string
data =
""
;
string
insertquery =
"insert into tblstudent(fname, mname, lname, emailid, password, contactno,hobby,address,countrycodenum)values(@fname,@mname,@lname,@emailid,@password,@contactno,@hobby,@address,@countrycodenum)"
;
SqlCommand cmd =
new
SqlCommand(insertquery, cn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue(
"@fname"
, fname);
cmd.Parameters.AddWithValue(
"@mname"
, mname);
cmd.Parameters.AddWithValue(
"@lname"
, lname);
cmd.Parameters.AddWithValue(
"@emailid"
, emailid);
cmd.Parameters.AddWithValue(
"@password"
, password);
cmd.Parameters.AddWithValue(
"@contactno"
, contactno);
cmd.Parameters.AddWithValue(
"@hobby"
, hobby);
cmd.Parameters.AddWithValue(
"@address"
, address);
cmd.Parameters.AddWithValue(
"@countrycodenum"
, countrycodenum);
int
i = cmd.ExecuteNonQuery();
if
(i > 0)
{
Console.WriteLine(
"Insert Successfully"
);
}
else
{
Console.WriteLine(
"Not Insert Successfully"
);
}
cn.Close();
return
data;
}
}
}
Web.config
<configuration>
<system.web>
<webServices>
<protocols>
<add name=
"HttpGet"
/>
<add name=
"HttpPost"
/>
</protocols>
</webServices>
Data is inserted in table But
I want to return json data when user click on Invoke button
?
Reply
Answers (
8
)
Excel data validate with Database data
MVC Transaction log