Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my webform page I need to insert a value to SQL table by using WCF Service. When I execute WCF file alone the value has been invoked then I connect to the webform page the value has not been inserted.

What I have tried:

Webform Code:

ASP.NET
<asp:TextBox value="" class="form-control" ID="Txtboxreg" runat="server" placeholder="Reg.No*">
  <asp:TextBox value="" class="form-control" ID="txtdr" runat="server" placeholder="Doctor*">
  <asp:TextBox class="form-control" ID="txtSalutation" runat="server" placeholder="Salutation*">
  <asp:TextBox class="form-control" ID="txtPatientName" runat="server" placeholder="PatientName*">
  <asp:TextBox class="form-control" ID="txtAge" runat="server" placeholder="Age*">
  <asp:TextBox class="form-control" ID="txtdob" runat="server" placeholder="DOB*">
  <asp:TextBox class="form-control" ID="txtGender" runat="server" placeholder="Gender*">
  <asp:TextBox class="form-control" ID="txtRemarks" runat="server" placeholder="Remarks">
  <asp:TextBox class="form-control" ID="txtAddress" runat="server" placeholder="Address*">
  <asp:TextBox class="form-control" ID="txtMobileNumber" runat="server" placeholder="MobileNumber*">
  <asp:TextBox class="form-control" ID="txtCity" runat="server" placeholder="City*">
  <asp:TextBox class="form-control" ID="txtCorporateName" runat="server" placeholder="CorporateName">
  <asp:TextBox class="form-control" ID="txtReferralDoctor" runat="server" placeholder="ReferralDoctor">
  <asp:Button ID="bbtb" runat="server" Text="SAVE" OnClick="Button1_Click" Width="87px" />
C# Code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using Wcftest.ServiceReference1;
using System.Web.UI.HtmlControls;
namespace Wcftest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        ServiceReference1.Service1Client objServiceClientobjService = new ServiceReference1.Service1Client();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            UserDetails userInfo = new UserDetails();
            userInfo.Reg_No = Txtboxreg.Text;
            userInfo.Doctor = txtdr.Text;
            userInfo.Salutation = txtSalutation.Text;
            userInfo.PatientName = txtPatientName.Text;
            userInfo.Age = txtAge.Text;
            userInfo.DOB = txtdob.Text;
            userInfo.Gender = txtGender.Text;
            userInfo.Address = txtAddress.Text;
            userInfo.MobileNumber = txtMobileNumber.Text;
            userInfo.City = txtCity.Text;
            userInfo.CorporateName = txtCorporateName.Text;
            userInfo.Remarks = txtRemarks.Text;
            userInfo.ReferralDoctor = txtReferralDoctor.Text;

            string result = objServiceClientobjService.InsertUserDetails(userInfo);
            LabelMessage.Text = result;
        }
    }
}

WCF Code

IService1.cs:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Wcftest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        string InsertUserDetails(UserDetails userInfo);
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
    public class UserDetails
    {
        string qReg_No = string.Empty;
        string qDoctor = string.Empty;
        string qSalutation = string.Empty;
        string qPatientName = string.Empty;
        string qAge = string.Empty;
        string qDOB = string.Empty;
        string qGender = string.Empty;
        string qAddress = string.Empty;
        string qMobileNumber = string.Empty;
        string qCity = string.Empty;
        string qCorporateName = string.Empty;
        string qRemarks = string.Empty;
        string qReferralDoctor = string.Empty;

        [DataMember]
        public string Reg_No
        {
            get { return qReg_No; }
            set { qReg_No = value; }
        }
        [DataMember]
        public string Doctor
        {
            get { return qDoctor; }
            set { qDoctor = value; }
        }
        [DataMember]
        public string Salutation
        {
            get { return qSalutation; }
            set { qSalutation = value; }
        }
        [DataMember]
        public string PatientName
        {
            get { return qPatientName; }
            set { qPatientName = value; }
        }
        [DataMember]
        public string Age
        {
            get { return qAge; }
            set { qAge = value; }
        }
        [DataMember]
        public string DOB
        {
            get { return qDOB; }
            set { qDOB = value; }
        }
        [DataMember]
        public string Gender
        {
            get { return qGender; }
            set { qGender = value; }
        }
        [DataMember]
        public string Address
        {
            get { return qAddress; }
            set { qAddress = value; }
        }
        [DataMember]
        public string MobileNumber
        {
            get { return qMobileNumber; }
            set { qMobileNumber = value; }
        }
        [DataMember]
        public string City
        {
            get { return qCity; }
            set { qCity = value; }
        }
        [DataMember]
        public string CorporateName
        {
            get { return qCorporateName; }
            set { qCorporateName = value; }
        }
        [DataMember]
        public string Remarks
        {
            get { return qRemarks; }
            set { qRemarks = value; }
        }
        [DataMember]
        public string ReferralDoctor
        {
            get { return qReferralDoctor; }
            set { qReferralDoctor = value; }
        }
    }
}

Service1.svc:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Wcftest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
        public string InsertUserDetails(UserDetails userInfo)
        {
            string Message;
            SqlConnection con = new SqlConnection("Data Source=.;initial catalog=xyz;user id=123;password=123");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into quickreg(qReg_No,qDoctor,qSalutation,qPatientName,qAge,qDOB,qGender,qAddress,qMobileNumber,qCity,qCorporateName,qRemarks,qReferralDoctor) values (@Reg_No,@Doctor,@Salutation,@PatientName,@Age,@DOB,@Gender,@Address,@MobileNumber,@City,@CorporateName,@Remarks,@ReferralDoctor)", con);
            cmd.Parameters.AddWithValue("@Reg_No", userInfo.Reg_No);
            cmd.Parameters.AddWithValue("@Doctor", userInfo.Doctor);
            cmd.Parameters.AddWithValue("@Salutation", userInfo.Salutation);
            cmd.Parameters.AddWithValue("@PatientName", userInfo.PatientName);
            cmd.Parameters.AddWithValue("@Age", userInfo.Age);
            cmd.Parameters.AddWithValue("@DOB", userInfo.DOB);
            cmd.Parameters.AddWithValue("@Gender", userInfo.Gender);
            cmd.Parameters.AddWithValue("@Address", userInfo.PatientName);
            cmd.Parameters.AddWithValue("@MobileNumber", userInfo.MobileNumber);
            cmd.Parameters.AddWithValue("@City", userInfo.City);
            cmd.Parameters.AddWithValue("@CorporateName", userInfo.CorporateName);
            cmd.Parameters.AddWithValue("@Remarks", userInfo.Remarks);
            cmd.Parameters.AddWithValue("@ReferralDoctor", userInfo.ReferralDoctor);

            int result = cmd.ExecuteNonQuery();
            if (result == 1)
            {
                Message = userInfo.Reg_No + " Details inserted successfully";
            }
            else
            {
                Message = userInfo.Reg_No + " Details not inserted successfully";
            }
            con.Close();
            return Message;
        }
    }
}
Posted
Updated 4-Jan-17 2:37am
v3
Comments
[no name] 4-Jan-17 8:10am    
We aren't a code debugging service. Learn how to debug your code and find the problem instead of dumping your entire code base in a posting and expecting people to do your work for you.
F-ES Sitecore 4-Jan-17 8:37am    
Learn to use the debugger so you can step through the code and inspect variables to try and track down what is going wrong.

In your InputUserDetails method, put all of the code in a try/catch block, and put a breakpoint in the catch part. If there's a problem with your sql stuff, it will be revealed in the exception.
 
Share this answer
 
Show Error what you are getting while inserting then we can provide solution.
 
Share this answer
 
Comments
Member 12656334 5-Jan-17 2:41am    
Error occurs on 'string result = objServiceClientobjService.InsertUserDetails(userInfo)'
ERROR:
{"The method or operation is not implemented."}
Saineshwar Bageri 5-Jan-17 2:48am    
Is web service and Insert data in database in same project then you can debug it.

Try doing a find in files in you entire solution to check for any other occurences of NotImplementedException
Member 12656334 5-Jan-17 4:46am    
i did the same thing already, but also its not working.
Member 12656334 5-Jan-17 4:49am    
check my stackoverflow link for reference:
http://stackoverflow.com/questions/41462242/how-to-insert-a-values-to-sql-table-using-wcf-service

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