运行方法

时间:2016-03-14 19:56:05

标签: javascript c# database datatable datarow

我正在尝试将数据发送到我的Provider类,并通过使用“addRow”方法向其添加行来操作数据表。当它去做方法时,我得到System.NullReferenceException任何建议我知道它在dt.newRow()之前崩溃所以一些数据表如何为空?

    class Provider
    {
    private DataTable dt;
    private DataRow dataRow;
    List<string> routes;
    private int _routeCount;
    private int count;

    public int RouteCount
    {
        get { return _routeCount; }

        set { _routeCount = value; }

    }

    private void Initial()
    {
        dt = new DataTable();
        dataRow = dt.NewRow();
        routes = new List<string>(); 
        _routeCount = 0;
        count = 0;

    }

    /*We Edit the table and create itfrom our excel data we got earlier*/
    public DataTable Load(string table)
    {
        DataTable d = null;
        dt = new DataTable();
        try
        {

            return d;
        }
        catch (Exception x)
        {
            MessageBox.Show(x.Message);
            return dt;
        }
    }

    public List<string> GetRoutes()
    {
        return routes;
    }

    public DataTable GetCustomers()
    {
        return dt;
    }

    public void addRow(string data)
    {
        Random rnd = new Random();
        Console.WriteLine("count = " + count);
        //Getting Routes loaded 
        try
        {
            if(count == 0)
            {
                dataRow = dt.NewRow();
            }
            switch (count)
            {
                case 0:
                    var maxC = Convert.ToInt32(999999999999);
                    var minC = Convert.ToInt32(100000000000);
                    dataRow["waybill"] = rnd.Next(minC, maxC) + "";
                    break;
                case 1:
                    dataRow["pieces"] = data;
                    break;
                case 2:
                    dataRow["receiver"] = data;
                    break;
                case 3:
                    dataRow["add"] = data;
                    break;
                case 4 :
                    dataRow["number"] = data;
                    break;


            }

            if(count == 5)
            {
                dt.Rows.Add(dataRow);
                count = 0;
            }
            count++;

           // return 0;
        }
        catch (Exception x)
        {
            MessageBox.Show(x.Message +"Call that no good tech guuy of yours after you try what you just did again ^_^" );
           // return 1;
        }
    }


    public void addRoute(string route)
    {
        Console.WriteLine(routes);
        if(routes != null)
        {

            routes.Add(route);
            _routeCount = routes.Capacity;
            // return 0;
        }



    }

    public DataTable Loading(string request)
    {
        Console.WriteLine("sooooooo:" + request);
        DataTable dTemp = null;
        string searchExpression = "waybill = " + request;
        DataRow[] foundRows = dt.Select(searchExpression);

        dTemp = new DataTable();

        foreach(DataRow row in foundRows)
        {
            dTemp.Rows.Add(row);
        }
        try
        {

            return dTemp;
        }
        catch (Exception x)
        {
            MessageBox.Show(x.Message);
            return dt;
        }
    }




}

}

1 个答案:

答案 0 :(得分:0)

更改了行

         Provider{
              private DataTable dt;
           }

         Provider{
              private DataTable dt= new dataTable();
           }

如果你在inital方法中以某种方式遇到null引用,我认为它没有被调用或者我的方法有误。但是,由于它从未被设置或未向其分配任何数据导致此问题。