如何修复 System.IndexOutOfRangeException:找不到 table 0 ASP.NET

How to fix System.IndexOutOfRangeException: Cannot find table 0 ASP.NET

  [WebMethod()]
        public DataTable insert_data_to_db_from_local(string partnumber, string srctcode, string dockcode,int pack,string error,string chk,string user,DateTime day,string ekb,string kbid)
        {
            SqlConnection objConn = new SqlConnection();
            SqlCommand objCmd = new SqlCommand();
            SqlDataAdapter dtAdapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            DataTable dt = null;
            string strConnString = null;
            StringBuilder strSQL = new StringBuilder();
            strConnString = "Server=localhost;UID=sa;PASSWORD=12345678;database=bds_pp_srct;Max Pool Size=400;Connect Timeout=600;";
            strSQL.Append("INSERT INTO Hanheld (Part_Number,SRCT_Code,Dock_Code,Package,Error_Code,Chk_Type,LogUser,LogDate,ekb_order_no,Kanban_ID) VALUES ('" + partnumber + "','" + srctcode + "','" + dockcode + "','" + pack + "','" + error + "','" + chk + "','" + user + "','" + day + "','" + ekb + "','" + kbid + "') ");
            //strSQL.Append(" WHERE [SRCT_Code] = '" + strCusID + "' ");
            objConn.ConnectionString = strConnString;
            var _with1 = objCmd;
            _with1.Connection = objConn;
            _with1.CommandText = strSQL.ToString();
            _with1.CommandType = CommandType.Text;
            dtAdapter.SelectCommand = objCmd;
            dtAdapter.Fill(ds);
            dt = ds.Tables[0];
            dtAdapter = null;
            objConn.Close();
            objConn = null;
            return dt;
        }

这个错误:

System.IndexOutOfRangeException: Cannot find table 0. at System.Data.DataTableCollection.get_Item(Int32 index)

试试这个

private DataTable dataTable = new DataTable();
string connString = @"query string here";
string query = "select table";

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();

SqlDataAdapter da = new SqlDataAdapter(cmd);        
da.Fill(dataTable);
conn.Close();
da.Dispose();

我认为您在代码中使用 DataSet 可能会有问题

因此您首先需要检查 DataSet 在 0 位置包含数据表的位置

例如

DataSet ds = new DataSet();
dtAdapter.Fill(ds);
if(ds != null && ds.Tables.Count > 0) {

//your logic

}
[WebMethod()]
    public void insert_data_to_db_from_local(string partnumber, string srctcode, string dockcode)
    {
        using (SqlConnection conn = new SqlConnection("Server=localhost;UID=sa;PASSWORD=12345678;database=Test;Max Pool Size=400;Connect Timeout=600;"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = @"INSERT INTO Hanheld(Part_Number,SRCT_Code,Dock_Code) VALUES(@partnumber,@srctcode,@dockcode)";
                cmd.Parameters.AddWithValue("@partnumber", partnumber);
                cmd.Parameters.AddWithValue("@srctcode", srctcode);
                cmd.Parameters.AddWithValue("@dockcode", dockcode);

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException e)
                {
                   // MessgeBox.Show(e.Message.ToString(), "Error Message");
                }

            }
        }
    }

This my Be fixed