Как вставить значения в таблицу SQL с помощью сервиса WCF?
На моей странице веб-формы мне нужно вставить значение в таблицу SQL с помощью службы WCF. Когда я выполняю только файл WCF, значение было вызвано, а затем я подключаюсь к странице веб-формы, значение не было вставлено.
Что я уже пробовал:
Код Веб-Формы:
<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# :
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
Файл 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.ВПВ:
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; } } }
[no name]
Мы не служба отладки кода. Научитесь отлаживать свой код и находить проблему, вместо того чтобы сбрасывать всю свою базу кода в публикацию и ожидать, что люди сделают вашу работу за вас.
F-ES Sitecore
Научитесь использовать отладчик, чтобы вы могли пройти через код и проверить переменные, чтобы попытаться отследить, что происходит не так.