0

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.

Webform Code

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

C# Code

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

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

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;
        }
    }
}

I have added the screenshot below for your easy understanding.

Error Screen

2
  • 1
    did you got any exception? Commented Jan 5, 2017 at 6:52
  • Is wcf service in different project , try to get some data by creating different service to test is service accessible here Commented Jan 5, 2017 at 10:34

1 Answer 1

1

Seems no issue in your WCF services.

Check your webforms config file .

the following link will help you Example

Sign up to request clarification or add additional context in comments.

4 Comments

still having the same pblm
you are getting any exceptions ?
your services and web forms in same project ?
ok fine,create one sample small test API with no arguments in your services. and try to call that API from your webforms .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.