JQGrid与ASP.net& MySQL不显示记录

时间:2013-12-21 04:46:04

标签: mysql asp.net json jqgrid

关于我的担心,请帮助我这是我的第三篇文章。我无法显示我的记录从数据库我正在使用mySQL数据库请大家帮我这个。先谢谢大家。

ItemCode.aspx

<script type="text/javascript">
        $(function () {
            $("#UsersGrid").jqGrid({
                url: 'modCust.ashx',
                datatype: 'json',
                height: 500,
                colNames: ['Item Code', 'Part Number', 'Short Description', 'Long Description',
 'Product Code', 'Group Code', 'Business Unit'],
                colModel: [
                    { name: 'ItemCode', width: 100, sortable: true},
                    { name: 'PartNumber', width: 100, sortable: true },
                    { name: 'ItemDescriptionShort', width: 100, sortable: true },
                    { name: 'ItemDescriptionLong', width: 100, sortable: true },
                    { name: 'ProductCode', width: 100, sortable: true },
                    { name: 'GroupCode', width: 100, sortable: true },
                    { name: 'BusinessUnit', width: 100, sortable: true },


                ],

                cmTemplate: { title: false },
                rowNum: 100,
                rowList: [100, 200, 300],
                pager: '#UsersGridPager',
                sortname: 'ItemCode',
                viewrecords: true,
                autowidth: true,
                sortorder: 'asc'
            });

            $("#UsersGrid").jqGrid('navGrid', '#UsersGridPager', { edit: false, add: false,
 del: false });
        });

</script>

ItemCode.ashx

public struct JQGridResults
    {
        public int page;
        public int total;
        public int records;
        public JQGridRow[] rows;
    }
    public struct JQGridRow
    {
        public string ItemCode;
        public string[] cell;
    }
    [Serializable]
    public class User
    {
        public string ItemCode { get; set; }
        public string PartNumber { get; set; }
        public string ItemDescriptionShort { get; set; }
        public string ItemDescriptionLong { get; set; }
        public string ProductCode { get; set; }
        public string GroupCode { get; set; }
        public string BusinessUnit { get; set; }
    }

    public class ItemCode : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            string _search = request["_search"];
            string numberOfRows = request["rows"];
            string pageIndex = request["page"];
            string sortColumnName = request["sidx"];
            string sortOrderBy = request["sord"];


            int totalRecords;
            Collection <User> users = GetUsers(numberOfRows, pageIndex, sortColumnName,
 sortOrderBy, out totalRecords);
            string output = BuildJQGridResults(users, Convert.ToInt32(numberOfRows),
 Convert.ToInt32(pageIndex), Convert.ToInt32(totalRecords));
            response.Write(output);
        }
        private string BuildJQGridResults(Collection <User> users, int numberOfRows, int
 pageIndex, int totalRecords)
        {

            JQGridResults result = new JQGridResults();
            List<JQGridRow> rows = new List<JQGridRow>();
            foreach (User user in users)
            {
                JQGridRow row = new JQGridRow();
                row.ItemCode = user.ItemCode;
                row.cell = new string[6];
                row.cell[0] = user.ItemCode;
                row.cell[1] = user.PartNumber;
                row.cell[2] = user.ItemDescriptionShort;
                row.cell[3] = user.ItemDescriptionLong;
                row.cell[4] = user.ProductCode;
                row.cell[5] = user.GroupCode;
                row.cell[6] = user.BusinessUnit;
                rows.Add(row);
            }
            result.rows = rows.ToArray();
            result.page = pageIndex;
            result.total = (totalRecords + numberOfRows - 1) / numberOfRows;
            result.records = totalRecords;
            JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength =
 Int32.MaxValue, RecursionLimit = 100 };
            // return new JavaScriptSerializer().Serialize(result);
            return serializer.Serialize(result);
        }


        private Collection<User> GetUsers(string numberOfRows, string pageIndex, string
 sortColumnName, string sortOrderBy, out int totalRecords)
        {
            Collection<User> users = new Collection<User>();
            string connectionString = "server=127.0.0.1;database=swans;uid=root;password='';
pooling=false";

            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    command.Connection = connection;
                    command.CommandText = "select
 ItemCode,PartNumber,ItemDescriptionShort,ItemDescriptionLong,ProductCode,GroupCode,BusinessUnit
 from tblitemcodefromqne;";
                    command.CommandType = CommandType.Text; // StoredProcedure;

                    MySqlParameter paramPageIndex = new MySqlParameter("@PageIndex",
 MySqlDbType.Int32);
                    paramPageIndex.Value = Convert.ToInt32(pageIndex);
                    command.Parameters.Add(paramPageIndex);

                    MySqlParameter paramColumnName = new MySqlParameter("@SortColumnName",
 MySqlDbType.VarChar, 50);
                    paramColumnName.Value = sortColumnName;
                    command.Parameters.Add(paramColumnName);

                    MySqlParameter paramSortorderBy = new MySqlParameter("@SortOrderBy",
 MySqlDbType.VarChar, 4);
                    paramSortorderBy.Value = sortOrderBy;
                    command.Parameters.Add(paramSortorderBy);

                    MySqlParameter paramNumberOfRows = new MySqlParameter("@NumberOfRows",
 MySqlDbType.Int32);
                    paramNumberOfRows.Value = Convert.ToInt32(numberOfRows);
                    command.Parameters.Add(paramNumberOfRows);

                    MySqlParameter paramTotalRecords = new MySqlParameter("@TotalRecords",
 MySqlDbType.Int32);
                    totalRecords = 0;
                    paramTotalRecords.Value = totalRecords;
                    paramTotalRecords.Direction = ParameterDirection.Output;
                    command.Parameters.Add(paramTotalRecords);


                    connection.Open();

                    using (MySqlDataReader dataReader = command.ExecuteReader())
                    {
                        User user;
                        while (dataReader.Read())
                        {
                            user = new User();
                            user.ItemCode = Convert.ToString(dataReader["ItemCode"]);
                            user.PartNumber = Convert.ToString(dataReader["PartNumber"]);
                            user.ItemDescriptionShort =
 Convert.ToString(dataReader["ItemDescriptionShort"]);
                            user.ItemDescriptionLong =
 Convert.ToString(dataReader["ItemDescriptionLong"]);
                            user.ProductCode = Convert.ToString(dataReader["ProductCode"]);
                            user.GroupCode = Convert.ToString(dataReader["GroupCode"]);
                            user.BusinessUnit = Convert.ToString(dataReader["BusinessUnit"]);
                            users.Add(user);
                        }
                    }
                    totalRecords = users.Count;

                }

                return users;
            }

        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }   

1 个答案:

答案 0 :(得分:0)

您已在网格中将数据类型定义为json。我在你的代码中找不到json reader。发布在您的网格中。

jsonReader : {  
 repeatitems:false,
 root: "rows",
     page: "page", 
     total: "total" ,
 records: "records" 
 },

另外用firebug检查,值是从数据库返回并使用json验证器检查它是否是有效的json,如http://jsonlint.com/