【发布时间】:2017-11-11 06:52:18
【问题描述】:
我是 ASP.Net 开发的新手,在将数据从 webService 发送到 webform 时遇到问题。我正在使用 JSON 方法,但在下面的代码行中我遇到了问题。编译器显示此错误消息。
"错误 3 类、结构或接口成员中的无效标记 '(' 声明
谁能告诉我是什么问题?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting;
/// <summary>
/// Summary description for GetStudent
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 GetStudent : System.Web.Services.WebService {
[WebMethod]
public GetStudent () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public void GetStudent () {
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename='C:\\Users\\Abdul Basit Mehmood\\Documents\\Visual Studio 2012\\WebSites\\WebSite1\\App_Data\\Database.mdf';Integrated Security= True");
List<StudentsList> stu = new List<StudentsList>();
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * From Student";
SqlDataReader DR = cmd.ExecuteReader();
while (DR.Read())
{
StudentsList student = new StudentsList();
student.Id = Convert.ToInt32(DR["Id"]);
student.name = DR["name"].ToString();
student.fname = DR["fname"].ToString();
student.email = DR["email"].ToString();
student.contact = DR["contact"].ToString();
student.Pname = DR["Pname"].ToString();
student.Cname = DR["Cname"].ToString();
StudentsList.Add(student);
}
JavaScriptSerializer js = new JavaScriptSerializer(StudentsList());
Context.Response.Write(js.Serializ(StudentsList));
}
}
【问题讨论】:
-
js.Serializ应该是js.serialize如果不是拼写错误 -
您应该向我们展示更多代码以获得帮助。这是我的猜测;在你的类定义中给 StudentList 一个默认值(使用构造函数)。
-
我,ev 将“js.Serializ”编辑为“js.Serialize”,但没有产生任何影响
-
请考虑将您的
StudentList类重命名为Student。这非常令人困惑,因为它代表单个学生实体。
标签: c# asp.net json ajax serialization