得到相同的错误System.IndexOutOfRangeException:找不到最近两天的表0

时间:2016-04-11 07:00:53

标签: c# asp.net

  

我正在尝试从此类填充gridview中的数据,但是获取错误无法找到表0.我还附加了错误的图像。

这是类名" class1"

中的代码
public DataSet get_rates()
{
    DataSet ds = new DataSet();
    OdbcDataAdapter ad;

    try
    {
        con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connect1"].ConnectionString);
        DataTable dt_all_server = new DataTable();
        //ad = new OdbcDataAdapter("SELECT * FROM switches where id='"+ip.ToString()+"'", con);
        ad = new OdbcDataAdapter("SELECT * FROM switches where server_type='vps'", con);
        ad.Fill(dt_all_server);
        ad.Dispose();



            DataTable dt_running = new DataTable();
            dt_running.Columns.Add("Name");
            dt_running.Columns.Add("ipaddress");
            dt_running.Columns.Add("status");
            dt_running.Columns.Add("Server");
            DataRow r;
            DataTable dt_gateway = new DataTable();
            dt_gateway.Columns.Add("Name");
            dt_gateway.Columns.Add("ipaddress");
            dt_gateway.Columns.Add("status");
            dt_gateway.Columns.Add("calls");
            DataTable dt_running_other = new DataTable();
            dt_running_other.Columns.Add("Name");
            dt_running_other.Columns.Add("ipaddress");
            dt_running_other.Columns.Add("status");
            dt_running_other.Columns.Add("calls");
            for (int i = 0; i < dt_all_server.Rows.Count; i++)
            {
                DataTable dt_switch_record = new DataTable();
            con = new OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver};server=" + dt_all_server.Rows[i]["ip"].ToString() + ";database=voipswitch;uid=" + dt_all_server.Rows[i]["username"].ToString() + ";pwd=" + dt_all_server.Rows[i]["password"].ToString() + "; option=3");
            ad = new OdbcDataAdapter("select a.ip_number,b.login,a.id_client from ipnumbers as a,Clientsip as b where a.id_client=b.id_client and a.id_client!='0'", con);

            ad.Fill(dt_switch_record);

            con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connect1"].ConnectionString);
            OdbcCommand cmd = new OdbcCommand();
            cmd.Connection = con;
            con.Open();
            for (int j = 0; j < dt_switch_record.Rows.Count; j++)
            {
                r = dt_running.NewRow();
                string status = "";
                cmd.CommandText = "select * from ip_inventory where ipaddress ='" + dt_switch_record.Rows[j]["ip_number"].ToString() + "' and IP_id='" + ip.ToString() + "' and id_client='" + dt_switch_record.Rows[j]["id_client"].ToString() + "'";
                int result = cmd.ExecuteNonQuery();
                if (result > 0)
                {
                        status = "OK";
                }
                else
                {
                    status = "Not match";
                }

                if (status.ToString() == "Not match")
                {
                    r[0] = dt_switch_record.Rows[j]["name"].ToString();
                    r[1] = dt_switch_record.Rows[j]["ip_number"].ToString();
                    r[2] = status;
                    r[3] = dt_all_server.Rows[i]["ip"].ToString();
                    dt_running.Rows.Add(r);
                }
                //r[0] = dt_switch_record.Rows[j]["login"].ToString();
                //r[1] = dt_switch_record.Rows[j]["ip_number"].ToString();
                ////r[1] = dt_switch_record.Rows[j]["ipaddress"].ToString().Replace(',', ' ').ToString();
                //r[2] = status;
                //dt_running.Rows.Add(r);
                //cmd.Dispose();
            }
            con.Close();
            ds.Tables.Add(dt_running);
            cmd.Dispose();


            // ================      Running Calls   ==========================
            #region Running Calls
            DataTable dt_switch_record1 = new DataTable();
            con = new OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver};server=" + dt_all_server.Rows[i]["ip"].ToString() + ";database=voipswitch;uid=" + dt_all_server.Rows[i]["username"].ToString() + ";pwd=" + dt_all_server.Rows[i]["password"].ToString() + "; option=3");
            con.Open();
            ad = new OdbcDataAdapter("select 'Answered' as no_calls,a.ip_number,b.login,a.id_client from calls as a,clientsip as b where a.id_client=b.id_client and a.Client_type='0' and a.call_start between '" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00' and '" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59' group by a.ip_number", con);
            ad.Fill(dt_switch_record1);
            con.Close();

            con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connect1"].ConnectionString);
            OdbcCommand cmd1 = new OdbcCommand();
            cmd1.Connection = con;
            con.Open();
            for (int j = 0; j < dt_switch_record1.Rows.Count; j++)
            {
                r = dt_gateway.NewRow();
                string[] ip_num = dt_switch_record1.Rows[j]["ip_number"].ToString().Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                string status = "";
                cmd1.CommandText = "select * from ip_inventory where ipaddress='" + ip_num[0].ToString() + "' and IP_id='" + ip.ToString() + "' and id_client='" + dt_switch_record1.Rows[j]["id_client"].ToString() + "'";
                int result = cmd1.ExecuteNonQuery();
                if (result > 0)
                {
                    status = "OK";
                }
                else
                {
                    status = "Not match";
                }
                r[0] = dt_switch_record1.Rows[j]["login"].ToString();
                //r[1] = dt_switch_record1.Rows[j]["ip_number"].ToString();
                r[1] = ip_num[0].ToString();
                r[2] = status;
                r[3] = dt_switch_record1.Rows[j]["no_calls"].ToString();
                dt_gateway.Rows.Add(r);
            }
            con.Close();
            cmd1.Dispose();
            #endregion


            // ================     Failed Calls   ============================
            #region Failed Calls
            DataTable dt_switch_record2 = new DataTable();
            con = new OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver};server=" + dt_all_server.Rows[i]["ip"].ToString() + ";database=voipswitch;uid=" + dt_all_server.Rows[i]["username"].ToString() + ";pwd=" + dt_all_server.Rows[i]["password"].ToString() + "; option=3");
            con.Open();
            ad = new OdbcDataAdapter("select 'NO' as no_calls,a.ip_number,b.login,a.id_client from callsfailed as a,clientsip as b where a.id_client=b.id_client and a.Client_type='0' and a.call_start between '" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00' and '" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59' group by a.ip_number", con);
            ad.Fill(dt_switch_record2);
            con.Close();

            con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connect1"].ConnectionString);
            OdbcCommand cmd2 = new OdbcCommand();
            cmd2.Connection = con;
            con.Open();
            for (int j = 0; j < dt_switch_record2.Rows.Count; j++)
            {
                r = dt_gateway.NewRow();
                string[] ip_num = dt_switch_record2.Rows[j]["ip_number"].ToString().Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                string status = "";
                cmd2.CommandText = "select * from ip_inventory where ipaddress='" + ip_num[0].ToString() + "' and IP_id='" + ip.ToString() + "' and id_client='" + dt_switch_record2.Rows[j]["id_client"].ToString() + "'";
                int result = cmd2.ExecuteNonQuery();
                if (result > 0)
                {
                    status = "OK";
                }
                else
                {
                    status = "Not match";
                    r[0] = dt_switch_record2.Rows[j]["login"].ToString();
                    //r[1] = dt_switch_record2.Rows[j]["ip_number"].ToString();
                    r[1] = ip_num[0].ToString();
                    r[2] = status;
                    r[3] = dt_switch_record2.Rows[j]["no_calls"].ToString();
                    dt_gateway.Rows.Add(r);
                }

            }
            con.Close();
            cmd2.Dispose();
            ds.Tables.Add(dt_gateway);
            #endregion



            // ================    Other Client    =============================

            DataTable dt_switch_record3 = new DataTable();
            con = new OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver};server=" + dt_all_server.Rows[i]["ip"].ToString() + ";database=voipswitch;uid=" + dt_all_server.Rows[i]["username"].ToString() + ";pwd=" + dt_all_server.Rows[i]["password"].ToString() + "; option=3");
            con.Open();
            if (ip.ToString() == "9" || ip.ToString() == "10" || ip.ToString() == "11" || ip.ToString() == "13" || ip.ToString() == "14")
            {
                ad = new OdbcDataAdapter("select 'Answered' as no_calls,a.ip_number,b.login,a.id_client,'Not match' as status from calls as a,clientse164 as b where a.id_client=b.id_client and a.Client_type='2' and a.call_start between '" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00' and '" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59' group by a.ip_number", con);
            }
            else
            {
                ad = new OdbcDataAdapter("select 'Answered' as no_calls,a.ip_number,b.login,a.id_client,'Not match' as status from calls as a,clientsshared as b where a.id_client=b.id_client and a.Client_type='32' and a.call_start between '" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00' and '" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59' group by a.ip_number", con);
            }
            ad.Fill(dt_switch_record3);
            con.Close();

            for (int j = 0; j < dt_switch_record3.Rows.Count; j++)
            {
                r = dt_running_other.NewRow();
                r[0] = dt_switch_record3.Rows[j]["login"].ToString();
                r[1] = dt_switch_record3.Rows[j]["ip_number"].ToString();
                r[2] = dt_switch_record3.Rows[j]["status"].ToString();
                r[3] = dt_switch_record3.Rows[j]["no_calls"].ToString();
                dt_running_other.Rows.Add(r);
            }

            }
            ds.Tables.Add(dt_running_other);
            ds.Tables.Add(dt_gateway);



    }
    catch
    {
        //ad.Dispose();
    }
    return ds;
}

这是我的.cs文件中的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bind();
    }
}

private void bind1()
{

    Class1 ad = new Class1();
    //ad.ip1 = ddl_ip.SelectedValue.ToString();
    //Literal_GATEWAY_IP.Text = ddl_ip.SelectedItem.Text.ToString();
    //Literal_running_IP.Text = ddl_ip.SelectedItem.Text.ToString();
    //Literal_running_IP.Text = ddl_ip.SelectedItem.Text.ToString();
    DataSet ds = ad.get_rates();

    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
    GridView2.DataSource = ds.Tables[1];
    GridView2.DataBind();
    GridView3.DataSource = ds.Tables[2];
    GridView3.DataBind();
}

Image of the error ....getting this error at the time of gridview bind

1 个答案:

答案 0 :(得分:0)

我想您可能没有更改数据层,架构层或存储过程之一。请检查并让我知道它是否有效...

我犯了类似的错误,然后我意识到我没有编辑架构层。