WCFのポストメソッドを使用すると、データをデータベースに格納した後、WCF GETを使用してデータをグリッドコントロールに表示します。ここでは、POSTメソッドを使用してデータベースにデータを格納するコードを記述しました。それは働いている。格納されたデータをグリッドコントロールにバインドするときにエラーが発生します。WCFメソッドでデータをグリッドにバインドする方法のデータがバインドされていません
以下のエラーが私が手:
はServiceModelクライアント構成セクションで契約「ServiceReference1.IService2」を参照して、デフォルトのエンドポイント要素を見つけることができませんでした。これは、アプリケーションに設定ファイルが見つからなかったか、またはこの契約に一致するエンドポイント要素がクライアント要素内に見つからなかった可能性があります。
//service.cs
[ServiceContract]
public interface IService1
{
[OperationContract()]
void AddStudent(StudentDetails sd);
}
[ServiceContract]
public interface IService2
{
[OperationContract]
Employee GetEmployee();
}
//service.svc
public class Service1 : IService1, IService2
{
[WebInvoke(UriTemplate = "ADDStudent", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
public void AddStudent(StudentDetails sd)
{
string constr = ConfigurationManager.ConnectionStrings["mine"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("insert into students values (@Studentname,@SDepartment,@SAddress,@SMobile)", con);
con.Open();
cmd.Parameters.AddWithValue("@Studentname", sd.StudentName);
cmd.Parameters.AddWithValue("@SDepartment", sd.SDepartment);
cmd.Parameters.AddWithValue("@SAddress", sd.SAddress);
cmd.Parameters.AddWithValue("@SMobile", sd.SMobile);
cmd.ExecuteNonQuery();
con.Close();
}
[WebGet(UriTemplate = "Empdetails", ResponseFormat = WebMessageFormat.Json)]
public Employee GetEmployee()
{
Employee emp = new Employee();
string constr = ConfigurationManager.ConnectionStrings["mine"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
//write code to bind data to a grid con5trol.
SqlCommand cmd = new SqlCommand("select * from students", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable st = new DataTable();
da.Fill(st);
emp.EmployeeTable = st;
return emp;
}
}
[DataContract()]
public class StudentDetails
{
[DataMember(Order = 0)]
public string StudentName { get; set; }
[DataMember(Order = 1)]
public string SDepartment { get; set; }
[DataMember(Order = 2)]
public string SAddress { get; set; }
[DataMember(Order = 3)]
public string SMobile { get; set; }
}
[DataContract]
public class Employee
{
[DataMember]
public DataTable EmployeeTable { get; set; }
}
//web.configファイル
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="mine" connectionString="Data Source=
(localdb)\v11.0;Initial Catalog=yash;Integrated Security=true"/>
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"
/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services> <!--1-->
<service behaviorConfiguration="ServiceBehaviour"
name="CreateService.Service1">
<endpoint behaviorConfiguration="Service1" address=""
binding="webHttpBinding" bindingConfiguration=""
contract="CreateService.IService1">
</endpoint>
<endpoint behaviorConfiguration="Service1" address=""
binding="webHttpBinding" bindingConfiguration=""
contract="CreateService.IService2">
</endpoint>
</service>
</services>
<behaviors> <!--2-->
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Service1">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
/////// /////// wcf consumption ////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Net;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using ConsumptionWcf.ServiceReference1;
namespace ConsumptionWcf
{
public partial class WebForm1 : System.Web.UI.Page
{
public class StudentDetails
{
public string StudentName { get; set; }
public string SDepartment { get; set; }
public string SAddress { get; set; }
public string SMobile { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
//Service1Employee semp = new Service1Employee();
Service2Client myservice = new Service2Client();
Employee emp = new Employee();
emp = myservice.GetEmployee();
DataTable dt = new DataTable();
dt = emp.EmployeeTable;
grid1.DataSource = dt;
grid1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
StudentDetails stu = new StudentDetails
{
// StudentName = TextBox1.Text,
// StudentName = Request["TextBox1"],
//StudentName = Request.Params["TextBox1"],
StudentName = Request.Form["TextBox1"],//Request Is Propery Of
Request Collections,Request Collection Object Is HttpRequest.
//Above Four Are The Methods To Collect Data At The Server
Side.
//SAddress = TextBox2.Text,
//SAddress = Request["TextBox2"],
//SAddress = Request.Params["TextBox2"],
SAddress = Request.Form["TextBox2"],
//SMobile = TextBox3.Text,
//SMobile = Request["TextBox3"],
//SMobile = Request.Params["TextBox3"],
SMobile = Request.Form["TextBox3"],
//SDepartment = TextBox4.Text
// SDepartment = Request["TextBox4"]
//SDepartment = Request.Params["TextBox4"]
SDepartment = Request.Form["TextBox4"]
};
DataContractJsonSerializer objseria = new
DataContractJsonSerializer(typeof(StudentDetails));
MemoryStream mem = new MemoryStream();
objseria.WriteObject(mem, stu);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0,
(int)mem.Length);
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadString("http://localhost:58369/Service1.svc/ADDStudent",
"POST", data);
Label1.Text = "Details saved using Rest service";
Response.Redirect("WebForm1.aspx");
DataContractJsonSerializer objseria1 = new
DataContractJsonSerializer(typeof(StudentDetails));
MemoryStream mem1 = new MemoryStream();
objseria.WriteObject(mem, stu);
string data1 = Encoding.UTF8.GetString(mem.ToArray(), 0,
(int)mem.Length);
WebClient webClient1 = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadString("http://localhost:58369/Service1.svc/Empdetails",
"GET", data);
}
}
、ここにあなたの全体のプログラムを投稿しないでください。動作しないコードを分離する。この質問は、投稿された方法は、「なぜこのコードが機能していないのか」に該当します。そして誰かがあなたがそこに残したプレースホルダーにコードとコードを投稿することを期待しないでください –