Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Jquery使用数据库中的数据填充datatable_C#_Jquery_Asp.net_Ajax - Fatal编程技术网

C# Jquery使用数据库中的数据填充datatable

C# Jquery使用数据库中的数据填充datatable,c#,jquery,asp.net,ajax,C#,Jquery,Asp.net,Ajax,我试图用ODBC从access数据库中获取的数据填充datatable。我需要使用ajax从asp.net web表单中以json字符串的形式调用函数,但在序列化对象错误时检测到循环引用 以下是我的html和web表单代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="Login.Employee" %> <!DOCTYPE html> &l

我试图用ODBC从access数据库中获取的数据填充datatable。我需要使用ajax从asp.net web表单中以json字符串的形式调用函数,但在序列化对象错误时检测到循环引用

以下是我的html和web表单代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="Login.Employee" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" />
        <script type="text/javascript" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $.ajax({
                    url: 'Employee.aspx/GetEmployee',
                    type: 'GET',
                    contentType: 'application/json; charset=utf-8',
                    datatype: 'json',
                    success: function (data) {
                        $('#datatable').dataTable({
                            data: data,
                            columns: [
                                { 'data': 'ID' },
                                { 'data': 'picture' },
                                { 'data': 'last_name' },
                                { 'data': 'first_name' },
                                { 'data': 'job_tile' },
                                { 'data': 'start_date' },
                                { 'data': 'end_date' }
                            ]
                        });
                    }
                });
            });
        </script>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <table id="datatable">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Picture</th>
                        <th>Last Name</th>
                        <th>First Name</th>
                        <th>Job Position</th>
                        <th>Start Date</th>
                        <th>End Date</th>
                    </tr>
                </thead>
            </table>
            <div>
            </div>
        </form>
    </body>
</html>
网页形式:

public static string MyConnection = "DSN=myAccess2";
public OdbcConnection MyConn = new OdbcConnection(MyConnection);
OdbcCommand myCommand;
OdbcDataAdapter adapter;

[WebMethod]
public void GetEmployee() 
{
    List<Employees> emp = new List<Employees>();
    var query = "SELECT * FROM employee";
    MyConn = new OdbcConnection(MyConnection);
    myCommand = new OdbcCommand(query, MyConn);
    MyConn.Open();
    OdbcDataReader reader = myCommand.ExecuteReader();
    while (reader.Read()) 
    {
        Employees empl = new Employees();
        empl.Id = Convert.ToInt32(reader["ID"]);
        empl.image = reader["picture"].ToString();
        empl.lastName = reader["last_name"].ToString();
        empl.firstName = reader["first_name"].ToString();
        empl.jobTitle = reader["job_title"].ToString();
        empl.StartDate = Convert.ToDateTime(reader["start_date"]);
        empl.EndDate = Convert.ToDateTime(reader["end_date"]);
        emp.Add(empl);
    }

    MyConn.Close();
    JavaScriptSerializer js = new JavaScriptSerializer();
    Context.Response.Write(js.Serialize(emp));
}
感谢您的帮助,谢谢

您需要返回列表,以便在ajax成功中获得数据

另外,通过添加以下内容[System.Web.Script.Services.ScriptMethodUseHttpGet=true],使WebMtehod保持静态,并允许GET调用webmethod


你为什么不把名单还给我?@Mairaj想给它上个课吗?我该如何设置图像呢?我的数据库有一个二进制格式的图像。使用bas64的图像也可以使用javascript呈现,这样您就可以为图像创建属性字符串。@Mairaj Ok,所以我将代码改为使用OdbcDataReader,并创建了一个Employee类,该类只有setter和getter。在我的read while循环中,我设置了我的值,现在当我运行我的表单时,它显示表中没有可用的数据。用新代码更新您的问题。对于转换为json部分,我似乎有点接近了。我将如何生成上下文。响应。。。。静止的或者有更好的方法吗?你所说的“我将如何制作上下文。回应…”是什么意思。。。。静态`?您不需要上下文。响应并将我提供的代码转换为json就足以让您在ajax成功中获得数据。哦,我正在考虑用json填充datatable。我想我不需要那么做吧?对于我的jquery部分?不,您必须像已经在做的那样填充datatable,只是用我的代码更改C代码。
[WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static List<Employees> GetEmployee() 
{
  List<Employees> emp = new List<Employees>();
  var query = "SELECT * FROM employee";
  MyConn = new OdbcConnection(MyConnection);
  myCommand = new OdbcCommand(query, MyConn);
  MyConn.Open();
  OdbcDataReader reader = myCommand.ExecuteReader();
  while (reader.Read()) 
  {
     Employees empl = new Employees();
     empl.Id = Convert.ToInt32(reader["ID"]);
     empl.image = reader["picture"].ToString();
     empl.lastName = reader["last_name"].ToString();
     empl.firstName = reader["first_name"].ToString();
     empl.jobTitle = reader["job_title"].ToString();
     empl.StartDate = Convert.ToDateTime(reader["start_date"]);
     empl.EndDate = Convert.ToDateTime(reader["end_date"]);
     emp.Add(empl);
   }

   MyConn.Close();
   return emp;
}