【问题标题】:Why showing error in Context.Response.Write()为什么在 Context.Response.Write() 中显示错误
【发布时间】: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


【解决方案1】:

以下是您需要修复 GetStudent 方法的方法 -

    [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();
            stu.Add(student); //Changed line: Changed variable name to stu which is the list variable declared earlier.

        }
    JavaScriptSerializer js = new JavaScriptSerializer(); //changed line : Removed the invalid parameter to the constructor of JavaScriptSerializer class
    Context.Response.Write(js.Serializ(stu)); //changed line : Used the correct stu list variable declared at the starting of the method.
}

注意:在命名班级时请格外小心。您的 StudentList 实体不是一个列表,而是一个具有各种属性的单个学生实体,例如 Idnamefname 等。请考虑将其重命名为 Student

为了验证您没有从数据库中获取任何无效字符,请先尝试运行以下代码是否正常?

class Program
{
        static void Main(string[] args)
        {
            List<StudentsList> stu = new List<StudentsList>();

            StudentsList student = new StudentsList();
            student.name = "Abdul Basit Mehmood";
            student.fname = "Abdul";
            stu.Add(student); 
            JavaScriptSerializer js = new JavaScriptSerializer(); 
            var serializedValue = js.Serialize(stu); 
         }
}

class StudentsList
{
    public string name;
    public string fname;
}

serializedValue 变量应该在快速观察窗口中显示一个有效的 JSON 字符串,如下所示:

【讨论】:

  • 谢谢帮助。但它不起作用。我已经按照你说的修改了代码,但错误仍然存​​在。 Contaxt.Response.Write"("js.Serialize"("stu")");.错误显示在上述行的双引号中。
  • 其次,我已经声明并使用学生列表作为 Default.aspx.cs 中的列表。"List allStudent= new List(); 使用 (MyDatabaseEntity de = new MyDatabaseEntity) { allStudent=de.TopStudent.ToList(); } return allStudent;"
  • 您现在在Context.Response.Write(js.Serializ(stu)); 代码中遇到什么错误?
  • “类、结构或接口成员声明中的无效标记 '('”适用于所有提到的括号。
  • 我们需要缩小来自您的数据库的数据是否良好。我编辑了我的帖子以添加新代码 sn-p。检查它是否工作正常,然后您需要调试您的 DB 阅读器部分,这可能会将无效字符推送到您在 while 循环中填充的 StudentList 对象属性中。
猜你喜欢
  • 2016-06-02
  • 2016-09-06
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2012-02-20
相关资源
最近更新 更多