简体   繁体   中英

An error occurred while receiving DataTable in wcf service response

I am developing a windows form application in c#. My database is handled in a wcf application. In my wcf application I have a class called lecturer which has a method called view lecturers() as follows. My purpose is to view the lecturers table in a grid view.

public DataTable viewLec()
        {

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["resourceAlloc"].ToString());

            DataTable dt = new DataTable();
            string vw = "select * from lecturers";

            SqlCommand cmd = new SqlCommand(vw,con);

            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);


            return dt;

        }

In my IService1.cs:

  [OperationContract]
        DataTable viewLecturer();

Service1.svc.cs:

public DataTable viewLecturer()
        {
            Lecturer lec = new Lecturer();
            return lec.viewLec();

        }

in my windows form application button click event,

private void button2_Click(object sender, EventArgs e)
{
    Service1Client obj = new Service1Client();   
    dataGridView1.DataSource = obj.viewLecturer();      
}

When I click the view button the above error occurs, I cannot understand why?

I don't know the exact error of your code, but first thing I want to mention is that using DataTable object as your Data Contract is not the best idea as it contains certain amount of extra data which usually is not consumed by the end applications. But anyway, in your particular case WCF will not be able to serialize your your object as it will not have table name which is mandatory for this class to perform serialization. Try to follow this way:

 DataTable dt = new DataTable();
 // Must set name for serialization to succeed.
 dt.TableName = "lecturers";

I don't see the actual error you have (you should post it to make your answer more clear), but at least this will help you to avoid serialization error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM